27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

rxvt<br />

bash$ echo $LOGNAME<br />

bozo<br />

bash$ echo $SHELL<br />

/bin/tcsh<br />

bash$ echo $TERM<br />

rxvt<br />

Positional Parameters<br />

$0, $1, $2, etc.<br />

positional parameters, passed from command line to script, passed to a function, or set to a variable<br />

(see Example 4−5 and Example 11−13)<br />

$#<br />

number of command line arguments [20] or positional parameters (see Example 34−2)<br />

$*<br />

All of the positional parameters, seen as a single word<br />

$@<br />

Same as $*, but each parameter is a quoted string, that is, the parameters are passed on intact, without<br />

interpretation or expansion. This means, among other things, that each parameter in the argument list<br />

is seen as a separate word.<br />

Example 9−6. arglist: Listing arguments with $* and $@<br />

#!/bin/bash<br />

# Invoke this script with several arguments, such as "one two three".<br />

E_BADARGS=65<br />

if [ ! −n "$1" ]<br />

then<br />

echo "Usage: `basename $0` argument1 argument2 etc."<br />

exit $E_BADARGS<br />

fi<br />

echo<br />

index=1<br />

echo "Listing args with \"\$*\":"<br />

for arg in "$*" # Doesn't work properly if "$*" isn't quoted.<br />

do<br />

echo "Arg #$index = $arg"<br />

let "index+=1"<br />

done<br />

# $* sees all arguments as single word.<br />

echo "Entire arg list seen as single word."<br />

echo<br />

index=1<br />

echo "Listing args with \"\$@\":"<br />

for arg in "$@"<br />

do<br />

Chapter 9. Variables Revisited 74

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!