Tuesday, October 6, 2009

Dollar sign ($) in shell script

- The dollar sign is used for variable substitution which means the value of a variable.

$ cat >var_example.sh
var="This is variable"
variable2=10
echo $var
echo $variable2

$ sh var_example.sh
This is variable
10

With preceding dollar sign $ variable name indicates the value the variable holds.

- In case of regular expression, dollar sign ($) means it matches ending position of a line or ending position of a string. A list of metacharacters are defined in http://arjudba.blogspot.com/2009/07/list-of-metacharacters-in-regular.html

- ${} is used for parameter substitution. ${var} is same as $var but comes with less ambiguity.
Example:

$ cat >par_subs
var=test
echo "This is ${var}-example."
echo This is $var-example.

$ sh par_subs
This is test-example.
This is test-example.

- With $* all of the positional parameters, can be seen as a single word. Positional parameters mean the parameter that we pass from command line to a script, passed to a function, or set to a variable. Note that, "$*" must be quoted.

- With $@ we can access positional parameter but each parameter is a quoted string. The parameters are passed on intact, without interpretation or expansion.
This means that each parameter in the argument list is seen as a separate word.

Example of both $* and $@:

$ cat >example_of_dollars.sh
#Check whether user has passed any arguments while running the script.
#;If first argument is NULL and does not exist then show echo about usage and quit.
if [ ! -n "$1" ]
then
echo "Usage: `basename $0` argument1 argument2 etc."
exit 3
fi

index=1 # Initialize index count to 1.

echo "Listing arguments with \"\$*\":" # Escape character \ is used to escape $ and "
for arg in "$*" # Note that "$*" doesn't work properly if "$*" isn't quoted.
do
echo "Argument #$index = $arg"
let "index+=1"
done # $* sees all arguments as single word.
echo "Entire arguments list seen as single word."

index=1 # Reset index count.

echo "Listing arguments with \"\$@\":" # Escape character \ is used to escape $ and "
for arg in "$@" # Note that "$@" is within double quote.
do
echo "Argument #$index = $arg"
let "index+=1"
done # $@ sees arguments as separate words.
echo "Argument list seen as separate words."
echo

index=1 # Reset index count.

echo "Listing arguments with \$* (unquoted):"
for arg in $*
do
echo "Argument #$index = $arg"
let "index+=1"
done # Unquoted $* sees arguments as separate words.
echo "Argument list seen as separate words."

exit 0


$ sh example_of_dollars.sh
Usage: example_of_dollars.sh argument1 argument2 etc.

$ sh example_of_dollars.sh test1 test2
Listing arguments with "$*":
Argument #1 = test1 test2
Entire arguments list seen as single word.
Listing arguments with "$@":
Argument #1 = test1
Argument #2 = test2
Argument list seen as separate words.

Listing arguments with $* (unquoted):
Argument #1 = test1
Argument #2 = test2
Argument list seen as separate words.


- With $? it is checked exit status of a command, a function, or of the script itself.
Example:

$ ls nofile_here
ls: cannot access nofile_here: No such file or directory

$ echo $?
2

$ ls welcome.sh
welcome.sh

$ echo $?
0


- The $$ variable holds the process ID of the script in which it appears.

No comments:

Post a Comment