【发布时间】:2018-11-11 00:32:55
【问题描述】:
这是我的示例 Bash 脚本 example.sh:
#!/bin/bash
# Reading arguments and mapping to respective variables
while [ $# -gt 0 ]; do
if [[ $1 == *"--"* ]]; then
v="${1/--/}"
declare $v
fi
shift
done
# Printing command line arguments through the mapped variables
echo ${arg1}
echo ${arg2}
现在,如果在终端中我按如下方式运行 bash 脚本:
$ bash ./example.sh "--arg1=value1" "--arg2=value2"
我得到正确的输出,例如:
value1
value2
完美!这意味着我可以使用 bash 脚本中的变量 ${arg1} 和 ${arg2} 来使用传递给参数 --arg1 和 --arg2 的值。
我现在对这个解决方案很满意,因为它符合我的目的,但是,任何人都可以提出更好的解决方案来在 bash 脚本中使用命名命令行参数?
【问题讨论】:
标签: bash command arguments line named