【问题标题】:bash script : echo does not recognize argumentsbash 脚本:echo 无法识别参数
【发布时间】:2022-11-22 08:50:16
【问题描述】:

我有一个带有这一行的 bash 脚本 (#!/bin/bash):

echo -ne "$display|\r"

但在执行时显示的是:

$ sh myScript.sh

-ne |

我试图用反撇号强制解释,并且在我的命令行中,-ne 选项工作得很好。

【问题讨论】:

  • 通过使用 sh 运行脚本,它可能没有在 bash 中运行。
  • 使文件可执行,并使用./myScript.sh运行它(不要拨打sh
  • echo 的不同版本(甚至是不同模式下的单个版本)在解释选项(如 -ne)方面完全不一致。最好完全避免使用它们,而改用 printf(在这种情况下,如果您想要解释 $display 中的转义序列,则为 printf '%b|\n' "$display",否则为 printf '%s|\n' "$display")。参见"Echo -n With String argument printing the '-n' part, even though not within quotation marks""Why is printf better than echo?"
  • 虽然上面的 cmets 是正确的(你没有使用 bash,我们不确切知道哪个echo 您正在使用的版本),也可能是您将 echo 别名为其他名称。首先,使用 bash 运行脚本(使用bash 脚本.sh).如果你仍然得到那个奇怪的输出,在你的脚本中做一个type echo,这样你就可以看到回声你正在执行。告诉我们您使用的是哪种操作系统也可能有意义。

标签: bash echo


【解决方案1】:

Ty 所有的回应。

什么起作用了:

  • 运行./script.sh
  • 运行bash script.sh
  • 使用 printf

所以实际上,如果使用任何 shell 二进制文件执行脚本,shebang 并不重要。

信息:

antoine?[01:36]:~$ cat script.sh 
#!/bin/bash

type echo
echo -ne "something"
antoine?[01:36]:~$ sh script.sh 
echo is a shell builtin
-ne something
antoine?[01:37]:~$ bash script.sh 
echo is a shell builtin
somethingantoine?[01:37]:~$ 
antoine?[01:37]:~$ ./script.sh 
echo is a shell builtin
somethingantoine?[01:37]:~$

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2015-09-10
    • 2016-03-04
    • 2018-08-11
    • 1970-01-01
    相关资源
    最近更新 更多