Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH - getting UID on shell script does not work [duplicate]

Tags:

bash

shell

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!

like image 759
Eric Lee Avatar asked Sep 12 '16 08:09

Eric Lee


People also ask

What does $@ do in bash script?

Symbol: $# The symbol $# is used to retrieve the length or the number of arguments passed via the command line. When the symbol $@ or simply $1, $2, etc., is used, we ask for command-line input and store their values in a variable.

What does $() mean in bash?

The dollar sign before the thing in parenthesis usually refers to a variable. This means that this command is either passing an argument to that variable from a bash script or is getting the value of that variable for something.

What does $2 do in bash?

$2 is the second command-line argument passed to the shell script or function. Also, know as Positional parameters.


1 Answers

$UID is a Bash variable that is not set under sh, that 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)

like image 184
SLePort Avatar answered Nov 18 '22 18:11

SLePort