重击:-c:第0行:意外的EOF

我尝试在终端上运行以下命令:

bash -c 's="test"; s=$(echo "word1  word2 " | awk '{print $1;}'); echo $s;'

它给我以下错误:

bash: -c: line 0: unexpected EOF while looking for matching `)'
bash: -c: line 1: syntax error: unexpected end of file
}); echo $s;: command not found

将脚本保存到文件中时不会发生此问题。

jsdyhy 回答:重击:-c:第0行:意外的EOF

通过-c传递给bash的参数使用单引号。这将“保护”内部的双引号,但不会保护在awk程序中用于隐藏“ $ 1”的引号。

来自bash Man

Enclosing  characters in single quotes preserves the literal value of
each character within the quotes.  A single quote may not occur between 
single quotes,even when preceded by a backslash.

鉴于只需要引用一个字符,请考虑对awk程序使用双引号,并转义'$'

bash -c 's="test"; s=$(echo "word1  word2 " | awk "{print \$1;}"); echo $s;'

Output:
word1
本文链接:https://www.f2er.com/3132317.html

大家都在问