O'Reilly logo

Mastering Bash by Giorgio Zarrelli

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Special variables

Let's see now some variables which have some spacial uses that we can benefit from:

${1}, ${n}

The first interesting variables we want to explore have a special role in our scripts because they will let us capture more than an argument on our first command-line execution. Have a look at this bunch of lines:

!/bin/bash    fistvariable=${1}secondvariable=${2}thirdvariable=${3}    echo "The value of the first variable is ${1}, the second is ${2}, the third is ${3}" 

Pay attention to $1, $2, $3:

zarrelli:~$ ./positional.sh The value of the first variable is , the second is , the third is   

First try, no arguments on the command line, we see nothing printed for the variables:

zarrelli:~$ ./positional.sh 1 2 3The value of the first ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required