April 2018
Beginner
284 pages
7h 3m
English
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 ...