Difference between revisions of "How to use positional parameters"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
(Created page with 'All command line parameters ( positional parameters ) are available via special shell variable $1, $2, $3,...,$9.')
 
Line 1: Line 1:
 
All command line parameters ( positional parameters ) are available via special shell variable $1, $2, $3,...,$9.
 
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:
 +
<source lang="bash">#!/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 : $3"
 +
echo "The value of all command-line arguments : $*"</source>
 +
Save and close the file. Rut it as follows:
 +
<source lang="bash">chmod +x cmdargs.sh
 +
./cmdargs.sh bmw ford toyota</source>

Revision as of 14:47, 5 September 2009

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 : $3"
echo "The value of all command-line arguments : $*"

Save and close the file. Rut it as follows:

chmod +x cmdargs.sh
./cmdargs.sh bmw ford toyota