You are previewing The Linux Command Line.

The Linux Command Line

Cover of The Linux Command Line by William E. Shotts Jr. Published by No Starch Press
O'Reilly logo

Chapter 32. Positional Parameters

One feature that has been missing from our programs is the ability to accept and process command-line options and arguments. In this chapter, we will examine the shell features that allow our programs to get access to the contents of the command line.

Accessing the Command Line

The shell provides a set of variables called positional parameters that contain the individual 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
"

This very simple script displays the values of the variables $0 through $9. When executed ...

The best content for your career. Discover unlimited learning on demand for around $1/day.