To access all the arguments which are passed to your script, you need positional
parameters. These are $1 for the rst argument, $2 for the second, and so on. You
can have up to nine parameters. To get the script name, use $0.
The following script foo.sh prints all arguments from 1 to 4:
#!/bin/sh
echo \"$1\" \"$2\" \"$3\" \"$4\"
If you execute this script with the above arguments, you get:
"Tux Penguin" "2000" "" ""
18.5.2 Using Variable Substitution
Variable substitutions apply a pattern to the content of a variable either from the
left or right side. The following list contains the possible syntax forms:
${VAR#pattern}
removes the shortest possible match from the left:
file=/home/tux/book/book.tar.bz2
echo ${file#*/}
home/tux/book/book.tar.bz2
${VAR##pattern}
removes the longest possible match from the left:
file=/home/tux/book/book.tar.bz2
echo ${file##*/}
book.tar.bz2
${VAR%pattern}
removes the shortest possible match from the right:
file=/home/tux/book/book.tar.bz2
echo ${file%.*}
/home/tux/book/book.tar
${VAR%%pattern}
removes the longest possible match from the right:
file=/home/tux/book/book.tar.bz2
echo ${file%%.*}
/home/tux/book/book
${VAR/pattern_1/pattern_2}
substitutes the content of
VAR
from the
pattern_1
with
pattern_2
:
file=/home/tux/book/book.tar.bz2
echo ${file/tux/wilber}
/home/wilber/book/book.tar.bz2
234 Start-Up