Skip to Content
Mastering Linux Shell Scripting - Second Edition
book

Mastering Linux Shell Scripting - Second Edition

by Mokhtar Ebrahim, Andrew Mallett
April 2018
Beginner
284 pages
7h 3m
English
Packt Publishing
Content preview from Mastering Linux Shell Scripting - Second Edition

Setting defaults

Using either the test command or the brackets, we can provide default values for variables, including command-line parameters. Taking the hello4.sh script we worked with earlier, we can modify it and set the name parameter if it is zero bytes:

#!/bin/bashname=$1[ -z $name ] && name="Anonymous"echo "Hello $name"exit 0

This code is functional but it is our choice how we code in the default value. We can, alternatively, assign a default value directly to the parameter. Consider the following command, where a default assignment is made directly:

name=${1-"Anonymous"}

In bash, this is known as parameter substitution and can be written in the following pseudo-code:

${parameter-default}

Wherever a variable (parameter) has not been ...

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
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 9781788990554Supplemental Content