重建 $@ 并在 Bash 中保持正确的单词分割

发布于 2024-12-19 12:47:03 字数 684 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天邊彩虹 2024-12-26 12:47:03

例如,您可以将参数放入数组中,

X=("$@")

根据您的喜好对其进行折磨。并恢复位置参数

set -- "${X[@]}"

或将其传递到其他地方

./elsewhere "${X[@]}"
./elsewhere "$@"

You can, for instance, put the parameters into array

X=("$@")

torture it to your liking. and restore back to positional parameters

set -- "${X[@]}"

or pass it elsewhere

./elsewhere "${X[@]}"
./elsewhere "$@"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文