如何删除环境变量的 "SC2154 "警告[已关闭]。

问题描述 投票:-1回答:1

如何删除shellcheck在inting shell脚本时的警告 "SC2154"?

#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

echo "proxy=$http_proxy" | sudo tee -a /etc/test.txt

警告是 "SC2154: http_proxy is referenced but not assigned."。

EDIT:我想用sudo把环境变量 "http_proxy "写到test.txt文件中。

bash shell lint shellcheck
1个回答
1
投票

详细描述在 警告的维基页面 你试图使用(可能)未初始化的变量 $http_proxy.

你可以 抑扬顿挫 的注释。

# shellcheck disable=SC2154
echo "proxy=$http_proxy" | ...

然而,更好的解决方案是修复脚本。在这里,你似乎认为 http_proxy 是一个环境变量。如果是这样的话,我们应该遵循命名惯例,把它叫做 "环境变量"。HTTP_PROXY. Shellcheck尊重这个惯例,不会显示警告。


0
投票

作为抑制警告的方法,你可以明确地展开 http_proxy 如果是null或未设置,则为nothing。

echo "proxy=${http_proxy:-}"
© www.soinside.com 2019 - 2024. All rights reserved.