我应该在环境路径名中使用引号吗? [英] Should I use quotes in environment path names?

查看:249
本文介绍了我应该在环境路径名中使用引号吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在清理我的所有配置文件,以使它们尽可能可读.我一直在寻找在导出~/.bashrc文件中的路径时使用引号的样式指南:

I'm in the process of cleaning up all my config files in an attempt to make them as readable as possible. I've been looking for a style guide on the use of quotes while exporting paths in, for example, a ~/.bashrc file:

export PATH="/users/me/path:$PATH"

vs

export PATH=/users/me/path:$PATH

Google 外壳样式指南建议避免使用路径名引号.相比之下,很多流行的点文件存储库(例如Zach Holman的在此)使用引号.在路径中使用引号是否有优势?

The Google shell style guide suggests avoiding quotes for path names. In contrast, a lot of the popular dotfiles repos (such as Zach Holman's here) use quotes. Are there any situations when it is an advantage to use quotes in the path?

推荐答案

帽子提示到 @gniourf_gniourf @chepner 寻求帮助.

Tip of the hat to @gniourf_gniourf and @chepner for their help.

tl;博士

为安全起见,请双引号:在所有情况下,它都适用于所有类似POSIX的外壳.

如果要添加基于~的路径,请选择性地保留~/ 不带引号的,以确保扩展~;例如:export PATH=~/"bin:$PATH". 有关变量分配中~扩展的规则,请参见下文.
或者,只需在单个双引号字符串内使用$HOME:
export PATH="$HOME/bin:$PATH"

If you want to add a ~-based path, selectively leave the ~/ unquoted to ensure that ~ is expanded; e.g.: export PATH=~/"bin:$PATH". See below for the rules of ~ expansion in variable assignments.
Alternatively, simply use $HOME inside a single, double-quoted string:
export PATH="$HOME/bin:$PATH"

注意:以下内容适用于 bashkshzsh ,但不适用于(大部分)严格符合POSIX的外壳,例如dash;因此,在定位/bin/sh时,必须将export 的RHS放在双引号中. [1]

NOTE: The following applies to bash, ksh, and zsh, but NOT to (mostly) strictly POSIX compliant shells such as dash; thus, when you target /bin/sh, you MUST double-quote the RHS of export.[1]

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆