Eric Lee Eric Lee - 2 months ago 14
Bash Question

BASH - getting UID on shell script does not work

Hi I have a question about bash.

and I'm new to it.

I made a file named "test.sh" and its contents is

#!/bin/bash
set -x
echo $UID
echo "$UID"
echo "$(id -u)"


and the result is blank!!
nothing shows up

However, when i just type "echo $UID" on terminal
it shows "1011"

is there anything i missed for bash?

Please help

UPDATED

bash version is 4.3.11 and I typed "sh test.sh" to execute.

and the result is

+ echo

+ echo

+ id -u
+ echo 1011
1011


thanks!

Answer

$UID is a Bash variable that is not set under sh, that's may be why it outputs blank lines.

Try bash test.sh or make your script executable with chmod u+x test.sh, the program defined in shebang will then be used (/bin/bash)

Comments