Skip to Content
Learning Linux Shell Scripting
book

Learning Linux Shell Scripting

by Ganesh Sanjiv Naik
December 2015
Beginner
306 pages
5h 2m
English
Packt Publishing
Content preview from Learning Linux Shell Scripting

Understanding default parameters

Many times we may pass certain parameters from the command line; sometimes, we may not pass any parameters at all. We may need to have certain default values to be initialized to certain variables.

We will understand this concept by the following script.

Create a default_argument_1.sh script as follows:

#!/bin/bash
MY_PARAM=${1:-default}
echo $MY_PARAM

Execute the script and check:

$ chmod +x default_argument_1.sh One
$ ./default_argument_1.sh One
One
$ ./default_argument_1.sh
default

Create another default_argument_2.sh script:

#!/bin/bash
variable1=$1
variable2=${2:-$variable1}
echo $variable1
echo $variable2

Output:

We executed the script two times:

  • When we passed two arguments, then variable1 was $1 and variable2
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Linux Shell Scripting - Second Edition

Learning Linux Shell Scripting - Second Edition

Ganesh Sanjiv Naik

Publisher Resources

ISBN: 9781785286216