Passing arguments to shell scripts

To pass an argument to a shell script from the command line, simply follow the shell command with your argument:
$ myShell.ksh arg1

Unix makes using the arguments in your shell script just as easy with some pre-defined variables:
$# - the number of arguments passed
$* - all arguments passed
$0 - the command run from the Unix prompt
$1 - $9 - argument one through nine passed ot the script

For instance, say I have the following shell script called myShell.ksh:
echo "This is myShell.ksh"
echo $#
echo $*
echo $0
echo $1
echo $2

Executing the script with the command
$ myShell.ksh bob smith

would produce the following output:
This is myShell.ksh
2
bob smith
myShell.ksh
bob
smith

Comments

Popular posts from this blog

Auditing user logon and logoff in Oracle

RMAN Restore vs. Recovery - what's the difference?