echo -e 在终端中工作但不在 bash 脚本中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10267852/
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 -e working in terminal but not in bash script
提问by Gazler
I developed and maintain a ruby gem called Githug and I am trying to write an automated test script for it. Githug basically manipulates a directory to put it into different states of a working git repository and you can execute git commands to "solve" the level.
我开发并维护了一个名为 Githug 的 ruby gem,我正在尝试为它编写一个自动化测试脚本。Githug 基本上操作一个目录,将其放入工作 git 存储库的不同状态,您可以执行 git 命令来“解决”级别。
One of the levels asks you for your git config details and I am doing the following:
其中一个级别要求您提供 git 配置详细信息,我正在执行以下操作:
#! /bin/sh
# ...snip
#level 4
FULL_NAME=$(git config --get user.name)
EMAIL=$(git config --get user.email)
echo -e "$FULL_NAME\n$EMAIL" | githug
When I execute from a bash script it (echo -e) doesn't work. But it does when I run it from the terminal.
当我从 bash 脚本执行时,它 (echo -e) 不起作用。但是当我从终端运行它时。
FULL_NAME=$(git config --get user.name)
EMAIL=$(git config --get user.email)
echo -e "$FULL_NAME\n$EMAIL" | githug
********************************************************************************
* Githug *
********************************************************************************
What is your name? What is your email?
Congratulations, you have solved the level
Why doesn't this work from the bash script?
为什么这在 bash 脚本中不起作用?
Thanks.
谢谢。
回答by user unknown
Wrong shebang:
错误的shebang:
#! /bin/sh
When it shall be a bash script, use
当它是一个 bash 脚本时,使用
#! /bin/bash
Bash has a buildin echo, which isn't 100% identic with /bin/echo.
Bash 有一个内置的 echo,它与 /bin/echo 不是 100% 相同的。
回答by VonC
As commented, printfis the preferred option, as illustrated in Git 2.14.x/2.15 (Q4 2017)
正如评论的那样,printf是首选选项,如 Git 2.14.x/2.15 (Q4 2017) 中所示
printf '%s\n%s' "$FULL_NAME" "$EMAIL"
See commit 1a6d468(17 Sep 2017) by Torsten B?gershausen (tboegi).
(Merged by Torsten B?gershausen -- tboegi--in commit 1a6d468, 21 Sep 2017)
请参阅Torsten B?gershausen ( ) 的commit 1a6d468(2017 年 9 月 17 日)。(由Torsten B?gershausen合并-- --在提交 1a6d468 中,2017 年 9 月 21 日)tboegitboegi
test-lint:echo -e(or-E) is not portableSome implementations of
echosupport the '-e' option to enable backslash interpretation of the following string.
As an addition, they support '-E' to turn it off.However, none of these are portable, POSIX doesn't even mention them, and many implementations don't support them.
A check for '
-n' is already done incheck-non-portable-shell.pl, extend it to cover '-n', '-e' or '-E'.
test-lint:(echo -e或-E)不可移植一些实现
echo支持 '-e' 选项以启用对以下字符串的反斜杠解释。
此外,他们支持“-E”将其关闭。然而,这些都不是可移植的,POSIX 甚至没有提到它们,而且许多实现都不支持它们。
-n已经在 中完成了对“ ”的检查check-non-portable-shell.pl,将其扩展到覆盖“-n”、“-e”或“-E”。

