what does it mean: ${0}, ${1}


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers what does it mean: ${0}, ${1}
# 1  
Old 04-27-2011
what does it mean: ${0}, ${1}

hi guys,

i have script like below:

if [ "${1}" != "" ]
then
DATETIME=${1}
fi

pls can anyone explain what does ${1} means? what if i have i have two arguments and have {2} added to the if cond? any links to get this claruified on the syntax?
# 2  
Old 04-27-2011
$1 is the first argument, $2 is the second argument, etc.

If the script is run with ./script.sh arg1 arg2 "arg 3" arg4
then $1 is arg1, $2 is arg2, $3 is arg 3, $4 is arg4, etc.

So they're just testing if it got called with any argument at all and, if it was, sets the variable DATETIME to it.

The special variable $# can be useful, it tells you how many arguments you have. He could have checked if [ $# -gt 0 ] instead of checking if $1 was empty.
# 3  
Old 04-27-2011
thanks...does it mean ${1} and $1 are the same?
# 4  
Old 04-27-2011
Yes, the same way $varname is the same as ${varname}.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 04-27-2011
While the curling braces are useless in your scripts, there are cases where they have a purpose and some other where they are required.

eg: $11 is, surprisingly, equivalent to ${1}1, i.e. the first parameter with character "1" appended while ${11} is the elevenths parameter.
Code:
$ set a b c d e f g h i j k l m n o p q r s t u v w x y z
$ echo $11
a1
$ echo ${11}
k

 
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question