Difference between revisions of "Sh"

From Secure Computing Wiki
Jump to: navigation, search
(get name of host directory for a script)
 
(added list of special variables)
Line 9: Line 9:
 
sh ~/test.sh
 
sh ~/test.sh
 
</pre>
 
</pre>
 +
 +
== Special Variable ==
 +
<pre>Variable ==> Meaning
 +
------------------------------
 +
$0 ==> Name of script
 +
$1 ==> Positional parameter #1
 +
$2-$9 ==> Positional parameters #2-#9
 +
${10} ==> Positional parameter #10
 +
$# ==> Number of positional parameters
 +
"$*" ==> All the positional parameters (as a single word)
 +
"$@" ==> All the positional parameters (as separate strings)
 +
${#*} ==> Number of command line parameters passed to script
 +
${#@} ==> Number of command line parameters passed to script
 +
$? ==> Return value
 +
$$ ==> Process ID (PID) of script
 +
$- ==> Flags passed to script (using set)
 +
$_ ==> Last argument of previous command
 +
$! ==> Process ID (PID) of last job run in background</pre>

Revision as of 10:10, 3 July 2008

To get the name of the directory in which a script resides, the following code example will work:

#!/bin/sh
echo "I live in `dirname $0`"

Simply put this in test.sh within your home directory, and do the following to see:

cd /
sh ~/test.sh

Special Variable

Variable ==> Meaning
------------------------------
$0 ==> Name of script
$1 ==> Positional parameter #1
$2-$9 ==> Positional parameters #2-#9
${10} ==> Positional parameter #10
$# ==> Number of positional parameters
"$*" ==> All the positional parameters (as a single word)
"$@" ==> All the positional parameters (as separate strings)
${#*} ==> Number of command line parameters passed to script
${#@} ==> Number of command line parameters passed to script
$? ==> Return value
$$ ==> Process ID (PID) of script
$- ==> Flags passed to script (using set)
$_ ==> Last argument of previous command
$! ==> Process ID (PID) of last job run in background