官术网_书友最值得收藏!

Understanding default parameters

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:

  1. When we passed two arguments, then variable1 was $1 and variable2 was $2.
  2. In the second case, when we passed only one argument, then $1 was taken as the default argument for $2. Therefore, variable1 was used as the default for variable2. If we do not give a second parameter, then the first parameter is taken as the default for the second parameter.
主站蜘蛛池模板: 普格县| 平顺县| 宜丰县| 忻城县| 沐川县| 安龙县| 沧源| 沙田区| 瑞金市| 通州市| 汝南县| 迭部县| 浮梁县| 扎赉特旗| 丹凤县| 山东省| 长武县| 庆元县| 蓬安县| 恭城| 北票市| 额尔古纳市| 吉安市| 泽普县| 永济市| 顺昌县| 临武县| 兴文县| 陆川县| 苍梧县| 合山市| 郸城县| 达日县| 金川县| 武定县| 岳阳县| 松潘县| 龙口市| 军事| 色达县| 晋江市|