命令行参数中的符号.. python, bash

1 投票
2 回答
2478 浏览
提问于 2025-04-15 22:44

我正在Linux上写一个用Python脚本来发推特的程序,想问一下,能不能直接用清晰的文本传递像“(”和“)”这样的符号,而不需要用引号包起来呢?

% ./twitterupdate this is me  #works fine
% ./twitterupdate this is bad :(( #this leaves a error on bash.

唯一的办法就是把文本放在--> ""里吗?比如说……

% ./twitterupdate "this is bad :(("  #this will reduce the ease of use for the script

有没有其他的解决办法呢?

2 个回答

1

这是一个链接,指向GNU的Bash手册,具体讲解了“引用”的内容。你可以通过这个链接了解在Bash中如何使用引用,帮助你更好地理解和使用命令行。

10

是的,给字符串加上引号是唯一的方法。Bash有自己的语法,有些字符是有特殊含义的。顺便说一下,单用双引号""是不够的,应该使用单引号。因为有些字符在普通的双引号里还是会被解释成其他意思:

$ echo "lots of $$"
lots of 15570
$ echo 'lots of $$'
lots of $$

撰写回答