Free Hosting Online for WorkStations

< Previous | Contents | Next >

Accessing The Command Line

The shell provides a set of variables called positional parameters that contain the individ- ual words on the command line. The variables are named 0 through 9. They can be demonstrated this way:


#!/bin/bash

# posit-param: script to view command line parameters echo "

\$0 = $0

\$1 = $1

\$2 = $2

\$3 = $3

\$4 = $4

\$5 = $5

\$6 = $6

\$7 = $7

\$8 = $8

\$9 = $9 "


A very simple script that displays the values of the variables $0-$9. When executed with no command line arguments, the result is this:



[me@linuxbox ~]$ posit-param


$0 = /home/me/bin/posit-param

[me@linuxbox ~]$ posit-param


$0 = /home/me/bin/posit-param


$1 =

$2 =

$3 =

$4 =

$5 =

$6 =

$7 =

$8 =

$9 =

$1 =

$2 =

$3 =

$4 =

$5 =

$6 =

$7 =

$8 =

$9 =


Even when no arguments are provided, $0 will always contain the first item appearing on the command line, which is the pathname of the program being executed. When argu- ments are provided, we see these results:


[me@linuxbox ~]$ posit-param a b c d


$0

=

/home/me/bin/posit-param

$1

=

a

$2

=

b

$3

=

c

$4

=

d

$5

=

$6

=

$7

=

$8

=

$9

=


Note: You can actually access more than nine parameters using parameter expan- sion. To specify a number greater than nine, surround the number in braces. For ex- ample ${10}, ${55}, ${211}, and so on.



 

Top OS Cloud Computing at OnWorks: