From 8e75de2275260b1e37ac59e4ef0e9d76a2cdcd20 Mon Sep 17 00:00:00 2001 From: leafee98 Date: Wed, 22 Feb 2023 22:23:50 +0800 Subject: [PATCH] =?UTF-8?q?update=20post:=20=E4=B8=BA=E5=8D=95=E4=B8=80?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=AE=BE=E7=BD=AE=E4=BB=A3=E7=90=86=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E7=9A=84=E7=BB=88=E7=AB=AF=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../为单一命令设置代理环境变量的终端函数.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/content/posts/为单一命令设置代理环境变量的终端函数.md b/content/posts/为单一命令设置代理环境变量的终端函数.md index a2bea75..5d36ba9 100644 --- a/content/posts/为单一命令设置代理环境变量的终端函数.md +++ b/content/posts/为单一命令设置代理环境变量的终端函数.md @@ -33,11 +33,27 @@ function proxyenv { HTTPS_PROXY=${proxy_dest} export proxy http_proxy https_proxy PROXY HTTP_RPOXY HTTPS_PROXY - eval "$@" + "$@" ) } ``` +> Update: 注意最后一条命令现在是 `"$@"` 而不是以前的 `eval "$@"`,这样才能够避免括号产生额外的意义。 +> +> 在 Bash 下使用 `help eval` 可以看到 `eval` 的作用,其会将所有作为参数的字符串合并为一个,然后直接传递给 shell 运行,所以这些字符串会再一次经过变量代换、命令行代换、引号移除等**展开**过程。 +> +> ``` +> $ help eval +> eval: eval [arg ...] +> Execute arguments as a shell command. +> +> Combine ARGs into a single string, use the result as input to the shell, +> and execute the resulting commands. +> +> Exit Status: +> Returns exit status of command or success if command is null. +> ``` + ### 针对 sudo 命令 在使用 `sudo` 命令时,它的默认行为是不保留任何当前 shell 下的环境变量,因此即便使用 `proxyenv sudo wget` 命令,也无法在 `wget` 中使用这些环境变量。一个临时的解决方案是使用 `sudo` 的 `--preserve-env` 参数,像下面这样