shell 如何删除环境变量的“SC2154”警告[已关闭]

uqcuzwp8  于 2023-08-07  发布在  Shell
关注(0)|答案(2)|浏览(143)

已关闭。此问题需要details or clarity。它目前不接受回答。
**希望改进此问题?**通过editing this post添加详细信息并阐明问题。

三年前就关了。
Improve this question
如何删除shellcheck的警告“SC2154”当linting shell脚本?

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

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

字符串
警告为“SC2154:http_proxy被引用但未分配。”
编辑:我想使用sudo将环境变量“http_proxy”写入test.txt文件。

dm7nw8vv

dm7nw8vv1#

一般来说,更好的解决方案是坚持约定并在ALL_CAPS中命名 your 环境变量。但是,在本例中,我理解http_proxy不是 * 您的 * 环境变量,而是由curlwget等程序指定的,因此您不能简单地重命名它。
您可以suppress any warning与评论:

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

字符串
这将忽略从该行开始的有关http_proxy的错误。后续的$http_proxy也不会给予错误,但其他变量会。
要在一个中心位置禁用多个变量的警告,请在脚本开头的代码段下面放置。警告:Shellcheck directives before the first command in your script will apply to the whole script。作为一种解决方法,将虚拟命令(例如true)。

#! /bin/bash
# Using "shellcheck disable=SC2154" here would ignore warnings for all variables
true
# Ignore only warnings for the three proxy variables
# shellcheck disable=SC2154
echo "$http_proxy $https_proxy $no_proxy" > /dev/null

sh7euo9m

sh7euo9m2#

如果http_proxy为null或未设置,则可以显式地将其扩展为nothing,以抑制警告:

echo "proxy=${http_proxy:-}"

字符串

相关问题