bash – 分配到位置参数

前端之家收集整理的这篇文章主要介绍了bash – 分配到位置参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在Bash中为位置参数赋值?我想为默认参数分配一个值:
if [ -z "$4" ]; then
   4=$3
fi

指示4不是命令.

内置的是设置位置参数的唯一方法
$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

您可能还需要查看参数count $#而不是测试-z.

原文链接:https://www.f2er.com/bash/386806.html

猜你在找的Bash相关文章