Providing parameter defaults

Within bash parameters, there are named spaces in the memory that allow us access to stored values. There are two types of parameters:

  • Variables
  • Special parameters

Special parameters are read-only and are pre-set by the shell. Variables are maintained by ourselves as well as bash. In general, when talking about syntax, bash will refer to variables by their family name of parameters.

Variables

Variables are one type of parameter. These can be set by the system or by ourselves. For example, $USER is a variable parameter that is set by the system but can be written by us. As such, it is not a read-only requisite of special parameters.

Special parameters

Special parameters are the second parameter type and are managed by the shell itself and are presented as read-only. We have come across these before in parameters, such as $0 but let's take a look at another $-. We can expand these parameters to gain an understanding of their use, using the echo command:

$ echo "My shell is $0 and the shell options are: $-"

From the annotated text that I have added, we can understand that the $- option represents the shell options that are configured. These can be displayed using the set -o command but it can be read programmatically using $-.

We can see this in the following screenshot:

Special parameters

The options set here are as follows:

  • h: This is hashall that allows for programs to be found using the PATH parameter
  • i: This shows that this is an interactive shell
  • m: This is short for monitor, which allows the use of the bg and fg commands to bring commands in and out of the background
  • B: This allows the brace expansion or mkdirdir{1,2} where we create dir1 and dir2
  • H: This allows history expansion or running commands, such as !501 to repeat commands from history

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/bash
name=$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 code, 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 declared and has a null value the default value will be used. If the parameter has been explicitly declared with a null value, we will use the :- syntax, as shown in the following example:

parameter=
${parameter:-default}

By editing the script now, we can create hello8.sh to make use of bash parameter substitution to provide the default value:

#!/bin/bash
#Use parameter substitution to provide default value
name=${1-"Anonymous"}
echo "Hello $name"
exit 0

This script and its output, both with and without a supplied value, are shown in the following screenshot:

Setting defaults

The hello8.sh script provides the functionality that we need with the logic built directly into the parameter assignment. The logic and assignment now are a single line of code within the script and it is a major step in keeping the script simple and maintaining the readability.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.75.165