Difference between revisions of "How to use positional parameters"
Jump to navigation
Jump to search
m (Text replacement - "</source>" to "</syntaxhighlight>") |
|||
(3 intermediate revisions by 3 users not shown) | |||
Line 7: | Line 7: | ||
==How Do I Access Command-Line Arguments== | ==How Do I Access Command-Line Arguments== | ||
Create a simple shell script called cmdargs.sh: | Create a simple shell script called cmdargs.sh: | ||
− | < | + | <syntaxhighlight lang="bash" >#!/bin/bash |
echo "The script name : $0" | echo "The script name : $0" | ||
echo "The value of the first argument to the script : $1" | echo "The value of the first argument to the script : $1" | ||
Line 15: | Line 15: | ||
echo "The value of all command-line arguments (\$* version) : $*" | echo "The value of all command-line arguments (\$* version) : $*" | ||
echo "The value of all command-line arguments (\$@ version) : $@" | echo "The value of all command-line arguments (\$@ version) : $@" | ||
− | </ | + | </syntaxhighlight> |
Save and close the file. Rut it as follows: | Save and close the file. Rut it as follows: | ||
− | < | + | <syntaxhighlight lang="bash" >chmod +x cmdargs.sh |
− | ./cmdargs.sh bmw ford toyota</ | + | ./cmdargs.sh bmw ford toyota</syntaxhighlight> |
Sample outputs: | Sample outputs: | ||
<pre>The script name : ./cmdargs.sh | <pre>The script name : ./cmdargs.sh | ||
Line 28: | Line 28: | ||
The value of all command-line arguments ($@ version) : bmw ford toyota</pre> | The value of all command-line arguments ($@ version) : bmw ford toyota</pre> | ||
Try the following examples: | Try the following examples: | ||
− | < | + | <syntaxhighlight lang="bash" >ls /tmp |
− | ./math 10 + | + | ./math 10 + 3 |
~/scripts/addzone cyberciti.com | ~/scripts/addzone cyberciti.com | ||
~/scripts/adddomain cyberciti.biz '74.86.48.99' '2607:f0d0:1002:11::4' | ~/scripts/adddomain cyberciti.biz '74.86.48.99' '2607:f0d0:1002:11::4' | ||
/etc/init.d/named reload | /etc/init.d/named reload | ||
− | /usr/local/etc/rc.d/jail restart cyberciti.biz</ | + | /usr/local/etc/rc.d/jail restart cyberciti.biz</syntaxhighlight> |
{| style="cellpadding="20" cellspacing="0" border="1" width="100%" | {| style="cellpadding="20" cellspacing="0" border="1" width="100%" | ||
! style="background:#ffdead;"| Shell script name ($0) | ! style="background:#ffdead;"| Shell script name ($0) | ||
Line 45: | Line 45: | ||
|./math | |./math | ||
|3 | |3 | ||
− | |10, +, 3 | + | |10, +, and 3 |
|- | |- | ||
|~/scripts/addzone | |~/scripts/addzone | ||
Line 69: | Line 69: | ||
=== Example: The Difference Between $@ and $* === | === Example: The Difference Between $@ and $* === | ||
Create a shell script called pizza.sh: | Create a shell script called pizza.sh: | ||
− | < | + | <syntaxhighlight lang="bash" >#!/bin/bash |
IFS=", " | IFS=", " | ||
echo "* Displaying all pizza names using \$@" | echo "* Displaying all pizza names using \$@" | ||
Line 76: | Line 76: | ||
echo "* Displaying all pizza names using \$*" | echo "* Displaying all pizza names using \$*" | ||
− | echo "$*"</ | + | echo "$*"</syntaxhighlight> |
Save and close the file. Run it as follows: | Save and close the file. Run it as follows: | ||
− | < | + | <syntaxhighlight lang="bash" >chmod +x pizza.sh |
− | ./pizza.sh Margherita Tomato Panner Gourmet</ | + | ./pizza.sh Margherita Tomato Panner Gourmet</syntaxhighlight> |
Sample outputs: | Sample outputs: | ||
<pre>* Displaying all pizza names using $@ | <pre>* Displaying all pizza names using $@ |
Latest revision as of 22:50, 29 March 2016
← Shell command line parameters • Home • Parameters Set by the Shell →
All command line parameters ( positional parameters ) are available via special shell variable $1, $2, $3,...,$9.
How Do I Access Command-Line Arguments
Create a simple shell script called cmdargs.sh:
#!/bin/bash
echo "The script name : $0"
echo "The value of the first argument to the script : $1"
echo "The value of the second argument to the script : $2"
echo "The value of the third argument to the script : $3"
echo "The number of arguments passed to the script : $#"
echo "The value of all command-line arguments (\$* version) : $*"
echo "The value of all command-line arguments (\$@ version) : $@"
Save and close the file. Rut it as follows:
chmod +x cmdargs.sh
./cmdargs.sh bmw ford toyota
Sample outputs:
The script name : ./cmdargs.sh The value of the first argument to the script : bmw The value of the second argument to the script : ford The value of the third argument to the script : toyota The number of arguments passed to the script : 3 The value of all command-line arguments ($* version) : bmw ford toyota The value of all command-line arguments ($@ version) : bmw ford toyota
Try the following examples:
ls /tmp
./math 10 + 3
~/scripts/addzone cyberciti.com
~/scripts/adddomain cyberciti.biz '74.86.48.99' '2607:f0d0:1002:11::4'
/etc/init.d/named reload
/usr/local/etc/rc.d/jail restart cyberciti.biz
Shell script name ($0) | Total number of arguments ($#) | Actual Command line argument ($1,..,$9) | ||
---|---|---|---|---|
ls | 1 | /tmp | ||
./math | 3 | 10, +, and 3 | ||
~/scripts/addzone | 1 | cyberciti.com | ||
~/scripts/adddomain | 3 | cyberciti.biz, 74.86.48.99, and 2607:f0d0:1002:11::4 | ||
/etc/init.d/named reload | 1 | reload | ||
/usr/local/etc/rc.d/jail | 2 | restart, and cyberciti.biz |
A Note About $@ and $*
- $@ expanded as "$1" "$2" "$3" ... "$n"
- $* expanded as "$1y$2y$3y...$n", where y is the value of $IFS variable i.e. "$*" is one long string and $IFS act as an separator or token delimiters.
Example: The Difference Between $@ and $*
Create a shell script called pizza.sh:
#!/bin/bash
IFS=", "
echo "* Displaying all pizza names using \$@"
echo "$@"
echo
echo "* Displaying all pizza names using \$*"
echo "$*"
Save and close the file. Run it as follows:
chmod +x pizza.sh
./pizza.sh Margherita Tomato Panner Gourmet
Sample outputs:
* Displaying all pizza names using $@ Margherita Tomato Panner Gourmet *Displaying all pizza names using $* Margherita,Tomato,Panner,Gourmet
← Shell command line parameters • Home • Parameters Set by the Shell →