相关疑难解决方法(0)

如何在bash中正确转义感叹号?

今天,我在尝试为 Twitter 编写密码生成器的代码时被当场抓获。

import string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))
Run Code Online (Sandbox Code Playgroud)

90 个字符。由于这是很多空闲空间,我决定提高标准并使其也可执行。

echo -e "#!/usr/bin/python\nimport string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))">pg;chmod +x pg;./pg
Run Code Online (Sandbox Code Playgroud)

139 个字符。很好,除了很明显 bash 窒息在感叹号上。

badp@delta:~$ echo -e "#!/usr/bin/python\nimport string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))">pg;chmod +x pg;./pg
bash: !/usr/bin/python\nimport: event not found
Run Code Online (Sandbox Code Playgroud)

讨厌的感叹号。“让我们逃离它,”我想!毕竟我有一个备用角色。

echo -e "#\!/usr/bin/python\nimport string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))">pg;chmod +x pg;./pg
Run Code Online (Sandbox Code Playgroud)

明显地...

badp@delta:~$ echo -e "#\!/usr/bin/python\nimport string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))">pg;chmod +x pg;./pg
./pg: line 2: syntax error near unexpected token `('
./pg: line 2: `import string as s,random;print ''.join(random.sample(s.letters+s.digits+s.punctuation,9))'
badp@delta:~$ cat pg
#\!/usr/bin/python …
Run Code Online (Sandbox Code Playgroud)

bash command-history quoting

12
推荐指数
1
解决办法
7577
查看次数

标签 统计

bash ×1

command-history ×1

quoting ×1