Pages

Monday, July 24, 2017

Command Line Arguments/Substitutions & Referencing Variables

Example HTML page
List of some of command line arguments

$0 - Name of the script from the command line
$1 - First command-line argument
$2 - Second command-line argument
$3 - Third command-line argument
$4 - Fourth command-line argument
$5 - Fifth command-line argument
$6 - Sixth command-line argument
$7 - Seventh command-line argument
$8 - Eighth command-line argument
$9 - Ninth command-line argument
$# - Number of command-line arguments
$*, $@ - All command-line arguments, separated with spaces
$? - Return status of previous command
$$ - PID of the current shell under which they are executing

Referencing Variables


a) ${var := word) - If variable value is not set or null then var is set to the value of record


b) ${var :? message) - If variable value is not set or null then Message is printed to standard error

c) ${var:-word} - If var is null or unset, word is substituted for var. The value of var does not change.

d) ${var:+word} - If var is set, word is substituted for var. The value of var does not change.

Part II


# Set the initial value.
myvar=abc
echo “Test 1 ======”
echo $myvar                    # abc
echo ${myvar}                  # same as above, abc
echo {$myvar}                  # {abc}

echo “Test 2 ======”
echo myvar                       # Just the text myvar
echo “myvar”                     # Just the text myvar
echo “$myvar”                   # abc
echo “\$myvar”                  # $myvar

echo “Test 3 ======”
echo $myvardef                # Empty line
echo ${myvar}def             # abcdef
echo ${myvar}{def}           # abc{def}

echo “Test 4 ======”
echo $myvar$myvar         # abcabc
echo ${myvar}${myvar}    # abcabc

echo “Test 5 ======”
# Reset variable value, with spaces
myvar=”a b c”
echo “$myvar”                  # a b c
echo $myvar                    # a b c

echo “Test 6 ======”
# Difference between single quotes and double quotes
myvar=abc
echo "$myvar"                 # abc    
echo '$myvar'                  # $myvar

echo " '$myvar' "             #  'abc'        ; ' ' has no special meaning inside " "
echo ' "$myvar" '             # "$myvar"  ;  " " is treated literally inside ' '

No comments:

Post a Comment