bash - 这是否等同于测试变量的存在?
问题描述
我正在挖掘我的 Mac 上的一些系统文件。在/etc/profile
我发现以下摘录:
if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi
我已经尝试echo "${x-no}"
过各种选择,x
似乎它正在打印任何存在的值x
(x
即已设置),no
否则。
这让我想知道:这个条件是否只是测试变量x
是否已设置?
进一步的问题:具体-
做什么?有没有更好的方法来测试变量是否已设置?
解决方案
这个条件是否只是测试变量 x 是否已设置?
是的,尽管它在不太可能发生的情况下会感到困惑BASH=no
。
究竟是做什么的?
这是man bash
:
[...] Omitting the colon results in a test only for a parameter that is un‐set.
${parameter:-word}
Use Default Values. If parameter is unset or null (see above),
the expansion of word is substituted. Otherwise, the
value of parameter is substituted.
有没有更好的方法来测试变量是否已设置?
是的:[ -v BASH ]
。但是,这是特定于 bash 的,因此它违背了在执行特定于 bash 的操作之前检查当前 shell 是否为 bash 的目的。
推荐阅读
- arrays - 删除包含相同 numpy 数组副本的 .txt 文件
- javascript - 地图不会达到最小缩放级别 openlayers6
- python - Python3:更新 numpy、scipy 和 scikit-learn 后没有名为“numpy.testing.decorators”的模块
- php - 如何使用 php 中选中的复选框搜索数据库?
- sql - 如何根据具有最后时间戳的行过滤 SQL 结果?
- python - 定义一个大小为 1 的字节对象
- r - 我将如何将 nrow() 用于多个条件?
- python - 计算 2D Numpy 数组的逐行移动平均值时处理 np.NaN
- ruby-on-rails - Capybara/Rspec - 使用 Ajax 请求在第二种情况下会话丢失
- python - 在 Python3 中从命令行和配置文件参数映射来自字典的变量