如何使用ZSH中第一个命令行参数的空间划界内容设置命令行arg

发布于 2025-02-09 09:13:17 字数 200 浏览 1 评论 0原文

我将在我编写的脚本中获得一个命令行参数,这本身将是实际命令行参数的空间界定列表。我想用这些参数设置当前脚本的参数。我将如何实现这一目标?

我想使用设置 -,但我不确定这将如何工作。

例如 给出我脚本的论点:-a -b -c

echo $1 # prints "-a -b -c"

I will be getting one command line argument in the script I'm writing which will itself be a space delimited list of the actual command line arguments. I'd like to set the arguments of the current script with these arguments. How might I accomplish that?

I'd like to use set -- but I'm not sure how this would work.

E.g.
Given arguments to my script: -a -b -c

echo $1 # prints "-a -b -c"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

伴我老 2025-02-16 09:13:17

您可以使用设置 - “ $ {(z)1}”。这将把$ 1分为单词,处理以相同的方式进行引用:

% cat script.zsh
#!/usr/bin/env zsh
set -- "${(z)1}"
for arg; do
  echo "==$arg=="
done

% ./script.zsh "-a -b -c -d'has spaces'"
==-a==
==-b==
==-c==
==-d'has spaces'==

如果您还想删除报价级别,请使用“ $ {(@q)$ {(z) )1}}“

You can do this with set -- "${(z)1}". This will split $1 into words, handling quoting the same way the shell itself does:

% cat script.zsh
#!/usr/bin/env zsh
set -- "${(z)1}"
for arg; do
  echo "==$arg=="
done

% ./script.zsh "-a -b -c -d'has spaces'"
==-a==
==-b==
==-c==
==-d'has spaces'==

If you also want to remove a level of quotes, use "${(@Q)${(z)1}}" instead.

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