bash 在 shell 脚本中使用 \c

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

Use of \c in shell scripting

linuxbashshellscripting

提问by seg.server.fault

I am editing someone else scripts and I see s/he has used this \cvery often like

我正在编辑其他人的脚本,我看到他/她\c经常使用这个

echo "bla bla \c"

which when used simply prints

使用时简单地打印

bla bla \c

on the screen.

屏幕上。

So, I was wondering why use it or may be I am missing something, because it is doing nothing at all ? I checked escape characters and I didn't find any such thing as well. Is there any special use of \c in shell scripts, mainly in bash and korn shells ?

所以,我想知道为什么要使用它,或者我可能遗漏了什么,因为它什么都不做?我检查了转义字符,我也没有找到任何这样的东西。在 shell 脚本中,主要是在 bash 和 korn shells 中,\c 有什么特殊用途吗?

回答by

The \c keeps the cursor on the same line after the end of the echo, but to enable it, you need the -e flag:

\c 在回声结束后将光标保持在同一行,但要启用它,您需要 -e 标志:

echo -e "bla bla \c"

回答by nik

I think the attempt is to terminate echo without a new line.

我认为尝试是在没有新行的情况下终止 echo。

If it does not work on your system, you can replace this way,

如果它在您的系统上不起作用,您可以通过这种方式替换,

echo "test \c"; echo " same line"

can become,

可以变成,

echo -n "test"; echo " same line"

An easier change will be (as suggested by Neil, +1 there),

更简单的更改将是(如Neil, +1所建议的那样),

echo -e "test \c"; echo " same line"

回答by nik

POSIXstates that:

POSIX指出:

\cSuppress the < newline > that otherwise follows the final argument in the output. All characters following the '\c' in the arguments shall be ignored.

\c禁止在输出中紧跟在最后一个参数之后的 < newline >。参数中 '\c' 后面的所有字符都将被忽略。

Basically a POSIX XSI-conformant echoutility should exit when it finds a \c, as is apparent from the following commands:

基本上符合 POSIX XSI 的echo实用程序在找到\c时应该退出,从以下命令可以看出:

echo -e "foo bar \c baz" "quux"

回答by Mark Rushakoff

No automatic line break, apparently:

显然没有自动换行符:

Use in UNIX Shells

UNIX Korn shells use the \c escape character to signal continuation (no automatic line break):

echo "Enter the path to the data file: \c"

在 UNIX Shell 中使用

UNIX Korn shell 使用 \c 转义字符来表示继续(没有自动换行符):

echo "输入数据文件的路径:\c"

回答by Geekou

In fact the behavior of 'echo' varies depending on the shell used.
With a simple Bourne shell, '\c' will be interpreted by default (so the cursor remains on the line).
With Bash, it is not interpreted by default (so the string '\c' is displayed), so you must add the '-e' option to enable it.

事实上,'echo' 的行为因所使用的外壳而异。
使用简单的 Bourne shell,'\c' 将被默认解释(因此光标保持在行上)。
使用 Bash,默认情况下不解释它(因此显示字符串 '\c'),因此您必须添加 '-e' 选项以启用它。

回答by Mithun

NAME echo - display a line of text

NAME echo - 显示一行文本

SYNOPSIS echo [SHORT-OPTION]... [STRING]... echo LONG-OPTION

概要 echo [SHORT-OPTION]... [STRING]... echo LONG-OPTION

DESCRIPTION Echo the STRING(s) to standard output.

描述 将 STRING(s) 回显到标准输出。

   -n     do not output the trailing newline

   -e     enable interpretation of backslash escapes

   -E     disable interpretation of backslash escapes (default)

   --help display this help and exit

   --version