bash 回声换行抑制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2645174/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:11:19  来源:igfitidea点击:

echo newline suppression

bashscriptingnewlineecho

提问by Happy Mittal

Why doesn't $echo '-n'write -non terminal although -nis written within single quotes ?

为什么不$echo '-n'-n在终端虽然-n写在单引号内?

回答by laalto

Because the quotes are processed by the shell and the echocommand receives plain -n. If you want to echo -n, you can e.g. printf '%s\n' -n

因为引号是由 shell 处理的,并且echo命令接收到普通的-n. 如果你想回声-n,你可以例如printf '%s\n' -n

回答by Erik Bitemo

I found that the following just works in bash:

我发现以下内容仅适用于 bash:

echo -n PRINT_THIS

So, you should put -n the first place.

所以,你应该把 -n 放在第一位。

回答by ghostdog74

you should try to use the more portable printfas far as possible.

你应该尽量使用更便携的printf

回答by kennytm

You could try

你可以试试

echo -e '5n'

回答by user unknown

Not quiete there:

那里不安静:

echo -e 'a-n\b\b\b '
 -n

surprisingly this works:

令人惊讶的是,这有效:

echo -e '-n\b'
-n

and here a solution I can explain:

在这里我可以解释一个解决方案:

echo "foobar" | sed 's/.*/-n/'
-n

回答by Pointy

The quotes don't help because ... ugh, it's hard to explain. The shell strips the quotes off before the "echo" command itself is evaluated, so by that time they don't matter.

引号没有帮助,因为......呃,很难解释。shell 在“echo”命令本身被评估之前去掉引号,所以到那时它们无关紧要。

Try this:

尝试这个:

echo - -n

That's not documented as working (I'm running Ubuntu Linux), but echois almost certainly a built-in to whatever shell you're using, so the man page is questionable anyway. It does work for me. (I'm running zshbecause I'm really suave and sophisticated).

这并没有记录在案(我正在运行 Ubuntu Linux),但echo几乎可以肯定它是您正在使用的任何 shell 的内置文件,因此手册页无论如何都是有问题的。它对我有用。(我跑步zsh是因为我真的很文雅又老练)。

edit: well it seems that the bashbuiltin editdoesn't behave that way. I don't know what to suggest; this:

编辑:好吧,似乎bash内置edit函数不会那样做。我不知道该建议什么;这个:

echo '' -n

will give you "-n" preceded by a space, which (depending on what you're doing) might be OK.

会给你一个空格前面的“-n”,这(取决于你在做什么)可能没问题。