Jus*_*tin 3 linux bash escaping
我正在尝试使用解释器运行 shell 片段bash
。使用该-c
标志允许我直接向命令行提供命令,而无需写入文件。这是我的使用所必需的。
工作示例:
$ bash -c 'free -m'
Run Code Online (Sandbox Code Playgroud)
问题是我想要运行的实际 shell 片段中有单引号。
find / -type f -size +50M -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
Run Code Online (Sandbox Code Playgroud)
我认为简单地转义引号就可以了:
$ bash -c 'find / -type f -size +50M -exec ls -lh {} \; | awk \'{ print $9 ": " $5 }\''
Run Code Online (Sandbox Code Playgroud)
但这是行不通的。它不执行该命令。
此外,同样的问题,我需要能够从命令行执行 Node.js,而不写入文件。
$ node -e 'console.log("hello");'
Run Code Online (Sandbox Code Playgroud)
上面的方法有效,但是:
$ node -e 'console.log('hello');'
and
$ node -e 'console.log(\'hello\');'
Run Code Online (Sandbox Code Playgroud)
两者都打破。
有想法吗?谢谢。
尝试这个:
$ bash -c $'find / -type f -size +50M -exec ls -lh {} \; | awk \'{ print $9 ": " $5 }\''
来自man bash
:
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped
characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are
decoded as follows:
\a alert (bell)
\b backspace
\e an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\nnn the eight-bit character whose value is the octal value nnn (one to three digits)
\xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
\cx a control-x character
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2845 次 |
最近记录: |