bash 中的回显参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7047253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
echo parameters in bash
提问by vvMINOvv
I'm writing a bash script but echo is taking all my parameters as string literals
我正在编写一个 bash 脚本,但 echo 将我的所有参数作为字符串文字
for example in this code:
例如在这段代码中:
echo -ne "Hello World"
produces -ne Hello World
产生 -ne Hello World
How do I get it to actually use the parameters "ne" ?
我如何让它实际使用参数“ne”?
Cheers.
干杯。
回答by armandino
Do you have #!/bin/bashdeclared at the top of the script? If not try to add it. It's probably executing under shand not bash, that's why you're seeing this behaviour.
你有没有#!/bin/bash在脚本的顶部声明?如果没有尝试添加它。它可能在sh而不是下执行bash,这就是您看到这种行为的原因。
回答by Keith Thompson
Consider using the printfcommand rather than echo; its behavior is much more uniform across different shells and operating systems.
考虑使用printf命令而不是echo; 它的行为在不同的外壳和操作系统中更加统一。
回答by Hola Soy Edu Feliz Navidad
In sh the echo is a command not a executable, so you can use /bin/echo to force the use of the executable and ensure that your parameters will work.
在 sh 中,echo 是命令而不是可执行文件,因此您可以使用 /bin/echo 强制使用可执行文件并确保您的参数有效。

