重建 $@ 并在 Bash 中保持正确的单词分割
我有一个 bash 函数,它将所有参数传递给另一个函数 b
,该函数在过去运行良好。
a () {
b "$@"
}
a -input /input -output "with blanks"
现在我想修改一些参数并将更新后的参数传递给b
。
我正在考虑将 $@
的副本重建为变量 ARGV
。我尝试过使用 ', ", 甚至 tab' 来保留字段。但它们都失败了。
你有什么好的想法吗?
$@ 的数据类型是什么以及为什么它正确捕获字段?
参考
使用 '
ARGV=""
while [ $# -ne 0 ] ; do
case "$1" in
-*)
ARGV="$ARGV $1 '$2'"
shift; shift; continue;;
esac
done
Using "
ARGV="$ARGV $1 \"$2\""
使用选项卡
IFS=$'\t'
ARGV="$ARGV $1 "$'\t'"$2"$'\t'
I have a bash function that passes all the parameters to another function b
which works well in the past.
a () {
b "$@"
}
a -input /input -output "with blanks"
Now I want modify some parameters and passes the updated parameters to b
.
I was thinking to reconstruct a copy of $@
to a variable ARGV
. I have tried to use ', ", and even tab' to keep the fields. But they all failed.
Do you have any good ideas?
What's the data type of $@ and Why it captures the fields correctly?
Reference
Using '
ARGV=""
while [ $# -ne 0 ] ; do
case "$1" in
-*)
ARGV="$ARGV $1 '$2'"
shift; shift; continue;;
esac
done
Using "
ARGV="$ARGV $1 \"$2\""
Using tab
IFS=
\t'
ARGV="$ARGV $1 "
\t'"$2"
\t'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
例如,您可以将参数放入数组中,
根据您的喜好对其进行折磨。并恢复位置参数
或将其传递到其他地方
You can, for instance, put the parameters into array
torture it to your liking. and restore back to positional parameters
or pass it elsewhere