bash 中双引号字符串中的单引号不受支持 [英] Single quotes inside a double-quoted string in bash not honored
问题描述
我正在尝试在 bash 中分配一个变量:
I am trying to assign a variable in bash:
assignssid="airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'"
当我当前这样做时,然后运行 echo "$assignssid"
,结果类似于以下内容:
When I currently do this, and then run echo "$assignssid"
, the result is something like the following:
airport -I | awk '/ SSID/ {print substr(-bash, index(-bash, ))}'
如何让变量与引号内的代码一起工作?
How do I get the variable to work with the code inside the quotes?
推荐答案
问题:单引号在双引号内不符合语法
当你有 "'$0'"
时,只有那些外引号是语法上的(并控制 shell 如何扩展值:内引号只是字面值,不会改变 $0
被处理.
The Problem: Single Quotes Aren't Syntactic Within Double Quotes
When you have "'$0'"
, only those outer quotes are syntactic (and control how the shell expands the value: The inner quotes are simply literal values, and don't change how $0
is treated.
如果您不想使用扩展名,请不要使用双引号.
If you don't want expansions to be honored, don't use double quotes.
# assign the directory separately, just to make the line shorter for easier reading
dir=/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/
# now, expand the directory name in double quotes, but use the $'' quoting style otherwise
assignssid="$dir"$'airport -I | awk \'/ SSID/ {print substr($0, index($0, $2))}\''
在 $''
里面,\'
是一个文字单引号(同样,\n
是一个换行符,\t
是制表符等),并且不会发生参数扩展($'$0'
与 '$0'
相同,而不是 "$0"
).
Inside $''
, \'
is a literal single quote (likewise, \n
is a newline, \t
is a tab, etc), and no parameter expansions take place ($'$0'
is the same as '$0'
, not "$0"
).
执行此操作后,您可以看到 echo
变量发出带有完整单引号内容的命令:
After doing this, you can see that echo
ing the variable emits your command with single-quoted contents intact:
$ echo "$assignssid"
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'
...或者,与您可能预期的用例更密切相关:
...or, more germane to your presumably-intended use case:
current_ssid=$(eval "$assignssid")
<小时>
字面答案(POSIX sh):'"'"'
让我们以一种不是 bash-only的方式来做第一个答案:
Literal Answer (POSIX sh): '"'"'
Let's do the first answer in a way that isn't bash-only:
dir=/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/
assignssid="$dir"'airport -I | awk '"'"'/ SSID/ {print substr($0, index($0, $2))}'"'"''
这个字符串的作用如下:
What this string does is as follows:
- 第一个
'
关闭单引号上下文 - 第一个
"
打开一个双引号上下文 - 下面的
'
是文字,因此成为你价值的一部分 - 以下
"
关闭双引号上下文 - 最后的
'
恢复了原来的单引号上下文
- The first
'
closes the single-quoted context - The first
"
opens a double-quoted context - The following
'
is literal, thus becomes part of your value - The following
"
closes the double-quoted context - The final
'
resumes the original single-quoted context
字符串变量根本不是这项工作的合适工具.相反,使用函数:
A string variable isn't the appropriate tool for this job at all. Instead, use a function:
airport_dir=/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/
assignssid() {
"$airport_dir"/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'
}
...以及此后:
current_ssid=$(assignssid)
这篇关于bash 中双引号字符串中的单引号不受支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!