<分区>
如何在 Bash 中为位置参数赋值?我想为默认参数赋值:
if [ -z "$4" ]; then
4=$3
fi
表示4不是命令。
<分区>
如何在 Bash 中为位置参数赋值?我想为默认参数赋值:
if [ -z "$4" ]; then
4=$3
fi
表示4不是命令。
最佳答案
内置的set
是设置位置参数的唯一方法
$ set -- this is a test
$ echo $1
this
$ echo $4
test
其中 --
防止看起来像选项的东西(例如 -x
)。
在您的情况下,您可能需要:
if [ -z "$4" ]; then
set -- "$1" "$2" "$3" "$3"
fi
但它可能会更清楚
if [ -z "$4" ]; then
# default the fourth option if it is null
fourth="$3"
set -- "$1" "$2" "$3" "$fourth"
fi
您可能还想查看参数计数 $#
而不是测试 -z
。
关于bash - 分配给位置参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13762370/