需要了解如何逃避命令行字符的bash提示 [英] bash tips needed for understanding how to escape characters in command-line
问题描述
我的命令行的bash知识缺少对特定区域:我经常忘记如何正确转义字符。今天,我想回应此字符串到一个文件:
My knowledge of commandline bash is missing on a particular area: I constantly forget how to properly escape characters. Today I wanted to echo this string into a file:
#!/bin/env bash
python -m SimpleHTTPServer
回声#/斌/ bash的ENV \\ npython -m SimpleHTTPServer!> server.sh&功放;&安培;搭配chmod + X server.sh
-bash:/斌/ ENV:找不到事件
echo "#!/bin/env bash\npython -m SimpleHTTPServer" > server.sh && chmod +x server.sh
-bash: !/bin/env: event not found
这是正确的!记住逃生
或bash会认为这是一个特殊的庆典活动命令
That's right: Remember to escape !
or bash will think it's a special bash event command.
但我不能得到正确的逃逸! \\!
收益率 \\!
在呼应字符串,也是如此 \\\\!
。
But I can't get the escaping right! \!
yields \!
in the echoed string, and so does \\!
.
此外, \\ n
不会转换为一个换行符。
Furthermore, \n
will not translate to a line break.
你有,它使我更容易理解转义规则的一些提示?
Do you have some general tips that makes it easier for me to understand escaping rules?
要很precise,我会接受一个答案,告诉我,我应该逃脱bash命令行上的字符?包括如何正确输出换行符和感叹号在我的例子。
To be very precise, I'll accept an answer which tells me which characters I should escape on the bash command line? Including how to correctly output newline and exclamation mark in my example.
推荐答案
单引号禁止所有逃避和替代:
Single quotes inhibit all escaping and substitution:
echo '$hello world!'
您可以交替或禁用报价,如果你需要的东西混合起来:
You can alternate or disable quoting if you need to mix things up:
echo '$5.00 on '"$horse"'!'
ls -ld ~/'$$'/*
国米preting逃跑也很容易:
Interpreting escapes is also easy:
echo $'Wake up!\7'
sort -n <<< $'4\n3\n8'
这篇关于需要了解如何逃避命令行字符的bash提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!