May 2018
Beginner
332 pages
7h 28m
English
Many times, we may pass certain parameters from the command line, but, sometimes, we may not pass any parameters at all. We may need to initialize certain default values to certain variables.
We will review this concept through the following script.
Create script default_argument_1.sh, as follows:
#!/bin/bash
MY_PARAM=${1:-default}
echo $MY_PARAM
Execute the script and check the output:
$ chmod +x default_argument_1.sh One$ ./default_argument_1.sh OneOne$ ./default_argument_1.shdefault
Create another default_argument_2.sh script:
#!/bin/bash
variable1=$1
variable2=${2:-$variable1}
echo $variable1
echo $variable2
The output is as follows:
We executed the script two times: