在shell脚本中使用\c。

时间:2021-09-05 00:29:42

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

我正在编辑别人的脚本,我看到s/他经常使用这个\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 shell中,\c有什么特殊用途吗?

6 个解决方案

#1


23  

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"

#2


9  

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"

#3


2  

POSIX states that:

POSIX州:

\c Suppress 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 echo utility should exit when it finds a \c, as is apparent from the following commands:

基本上,当它找到\c时,一个POSIX xsi -一致性回声实用程序应该退出,从以下命令可以明显看出:

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

#4


1  

No automatic line break, apparently:

没有自动断线,显然:

Use in UNIX Shells

UNIX shell中使用

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

UNIX Korn shell使用\c转义字符进行信号延续(没有自动换行):

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

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

#5


0  

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”的行为会根据所使用的shell而变化。使用一个简单的Bourne shell, '\c'将被默认解释(因此光标仍然在行上)。对于Bash,默认情况下不会解释它(因此会显示字符串'\c'),因此必须添加'-e'选项来启用它。

#6


0  

NAME echo - display a line of text

名称回波-显示一行文本

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

剧情简介回波(空头期权)……[STRING]…回声LONG-OPTION

DESCRIPTION Echo the STRING(s) to standard output.

描述将字符串返回到标准输出。

   -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

#1


23  

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"

#2


9  

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"

#3


2  

POSIX states that:

POSIX州:

\c Suppress 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 echo utility should exit when it finds a \c, as is apparent from the following commands:

基本上,当它找到\c时,一个POSIX xsi -一致性回声实用程序应该退出,从以下命令可以明显看出:

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

#4


1  

No automatic line break, apparently:

没有自动断线,显然:

Use in UNIX Shells

UNIX shell中使用

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

UNIX Korn shell使用\c转义字符进行信号延续(没有自动换行):

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

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

#5


0  

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”的行为会根据所使用的shell而变化。使用一个简单的Bourne shell, '\c'将被默认解释(因此光标仍然在行上)。对于Bash,默认情况下不会解释它(因此会显示字符串'\c'),因此必须添加'-e'选项来启用它。

#6


0  

NAME echo - display a line of text

名称回波-显示一行文本

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

剧情简介回波(空头期权)……[STRING]…回声LONG-OPTION

DESCRIPTION Echo the STRING(s) to standard output.

描述将字符串返回到标准输出。

   -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