加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 我是否需要引用命令替换?

发布时间:2020-12-15 22:03:56 所属栏目:安全 来源:网络整理
导读:根据 Google Shell Style Guide,我应该: Always quote strings containing variables,command substitutions,spaces or shell meta characters,unless careful unquoted expansion is required. 也许我误解了“命令替换”的含义,但我想知道是否需要在以下示
根据 Google Shell Style Guide,我应该:

Always quote strings containing variables,command substitutions,spaces or shell meta characters,unless careful unquoted expansion is required.

也许我误解了“命令替换”的含义,但我想知道是否需要在以下示例中使用引号:

VAR="$(echo foo bar)"

解决方法

$(echo foo bar)确实是命令替换.在此特定示例中,您不需要双引号,因为变量赋值会为其右侧创建“双引号上下文”,因此VAR = $(…)等效于VAR =“$(…)”.

在bash中,导出VAR = $(…)或声明VAR = $(…)时不需要双引号.但是你需要在其他一些实现中使用双引号,例如破折号.

你确实需要在env VAR = $(…)somecommand中使用双引号,在make VAR = $(…)等中.这不是使双引号可选的等号,而是等号被解析的事实shell作为一个任务.

有a few other contexts where the double quotes are optional,但你不能错过简单的规则:always use double quotes around variable and command substitutions除非you want the split+glob operator.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读