Embedded Commands   «Prev 

Inserting a Carriage Return into a command

How do I insert a carriage return into a Unix command?
A carriage return character in a Unix command is typically represented as \r. However, its usage may vary depending on the specific circumstances.

In a Shell Script:

If you are writing a shell script and want to include a carriage return, you can do so by using the echo command with the -e flag. This flag enables interpretation of backslash escapes. Here's an example:
echo -e "Line 1\rLine 2"

The output of this command will be Line 2, because the carriage return (\r) moves the cursor back to the beginning of the line, and "Line 2" overwrites "Line 1".
In a Text File:
If you are editing a text file and you want to include a carriage return character, you can use the printf command and redirect the output to the file:
printf 'Line 1\rLine 2\n' >> file.txt

This command appends "Line 1", a carriage return, "Line 2", and a newline to the file file.txt.
In a Terminal Command:
If you want to insert a carriage return in a command you're typing in the terminal (for instance, to split a long command over multiple lines for readability), you can use the backslash (\) at the end of a line:
command -option1 -option2 \
-option3 -option4

This allows you to split a single command over multiple lines. The shell treats it as one continuous line, ignoring the newline character after the backslash.
Please note that the usage of the carriage return (\r) in Unix may not yield the same results as in other systems, such as Windows, where it is commonly used in combination with a line feed (\n) to start a new line. In Unix-based systems, the line feed (\n) is typically used on its own to start a new line.
You can insert a literal character into a UNIX command by using a sequence of two characters. The first character will always be control V.
Control V asks the shell to insert the next character you type literally into your command and not to interpret it as something special. To insert a literal carriage return into a command, type control V, then type a carriage return. This looks curious on your screen as you type.

Escaping

Recall that the ssh client has an escape sequence feature. By typing a particular character, normally a tilde (~), immediately after a newline or carriage return, you can send special commands to ssh: terminate the connection, suspend the connection, and so forth. But sometimes the default escape character can cause a problem. Suppose you connect by ssh from host A to host B, then from host B to host C, and finally from host C to host D, making a chain of ssh connections.
(We represent the machines shell prompts as A$, B$, C$, and D$.)

A$ ssh B
...
B$ ssh C
...
C$ ssh D
...
D$

While logged onto host D, you press the Return key, then ~ ^Z (tilde followed byControl-Z) to suspend the connection temporarily. Well, you have got three ssh connections active, so which one gets suspended? The first one does, and this escape sequence brings you back to the host A prompt. Well, what if you want to escape back to host B or C ?
There are two methods, one with forethought and one on the spur of the moment.