我写了一个辅助脚本(Z shell(可执行
当执行shell脚本时,以下是结果:
参数
当我在Z shell中调用打印(echo)的命令时,它可以正常工作。请参考:
我的当前修复在shell文件中是这样的:
为什么变量的值被传递/解释得不同呢?我在这里漏掉了什么吗?
Z shell 版本:5.8.1 (x86_64-ubuntu-linux-gnu)
zsh
)方言),用于安装Python的Poetry软件包管理器。一个奇怪的问题出现了,当在(a)shell脚本中运行时,相同的shell命令被解释为不同的方式,与(b)直接在Z shell中运行时不同。脚本的摘录如下:#!/bin/zsh
set -x
INSTALL_SPECIFIC_VERSION="--version 1.6.1"
# Why does this not work?
echo "curl -sSL URL | python3 - $INSTALL_SPECIFIC_VERSION"
# will print:
# curl -sSL URL | python3 - --version 1.6.1
curl -sSL URL | python3 - "$INSTALL_SPECIFIC_VERSION"
# The fix I use which works but I don't understand why, is:
# curl -sSL URL | python3 - $(printf "%s" "$INSTALL_SPECIFIC_VERSION")
set +x
当执行shell脚本时,以下是结果:
➜ /usr/bin/zsh test_script.zsh
+test_script.zsh:3> INSTALL_SPECIFIC_VERSION='--version 1.6.1'
+test_script.zsh:5> echo 'curl -sSL URL | python3 - --version 1.6.1'
curl -sSL URL | python3 - --version 1.6.1
+test_script.zsh:8> curl -sSL URL
+test_script.zsh:8> python3 - '--version 1.6.1'
usage: - [-h] [-p] [--version VERSION] [-f] [-y] [--uninstall] [--path PATH] [--git GIT]
-: error: unrecognized arguments: --version 1.6.1
+test_script.zsh:9> set +x
参数
--version 1.6.1
无法被远程调用的shell脚本解释。当我在Z shell中调用打印(echo)的命令时,它可以正常工作。请参考:
➜ /usr/bin/zsh -c "curl -sSL URL | python3 - --version 1.6.1"
Retrieving Poetry metadata
The latest version (1.6.1) is already installed.
我的当前修复在shell文件中是这样的:
# The fix I use which works but I don't understand why, is:
curl -sSL URL | python3 - $(printf "%s" "$INSTALL_SPECIFIC_VERSION")
为什么变量的值被传递/解释得不同呢?我在这里漏掉了什么吗?
Z shell 版本:5.8.1 (x86_64-ubuntu-linux-gnu)
python3 - "$var"
只接收到2个参数(-
和$var
的值),而python3 - --version 1.6.1
是使用3个参数(-
,--version
和1.6.1
)运行python。你的脚本和你的交互调用是不等价的:等价的交互调用是python3 - '--version 1.6.1'
。 - undefinedpython3 - $INSTALL_SPECIFIC_VERSION
。 - undefinedINSTALL_SPECIFIC_VERSION=--version 1.6.1
。如果你是指这个问题,我会这样回答。请随时添加其他答案。 - undefinedINSTALL_SPECIFIC_VERSION=--version 1.6.1
是无意义的,你应该会得到一个错误 _zsh: command not found: 1.6.1_。一般来说,如果你写类似A=B C
的语句,zsh 会尝试在一个环境中运行程序C
,其中变量A
的值被设置为B
。 - undefined