Escape in linux shell

5.2. Escaping

Escaping is a method of quoting single characters. The escape ( \ ) preceding a character tells the shell to interpret that character literally.

With certain commands and utilities, such as echo and sed , escaping a character may have the opposite effect — it can toggle on a special meaning for that character.

Special meanings of certain escaped characters

means «alert» (beep or flash)

translates to the octal ASCII equivalent of 0xx

Example 5-2. Escaped Characters

#!/bin/bash # escaped.sh: escaped characters echo; echo # Escaping a newline. # —————— echo «» echo «This will print as two lines.» # This will print # as two lines. echo «This will print \ as one line.» # This will print as one line. echo; echo echo «=============» echo «\v\v\v\v» # Prints \v\v\v\v literally. # Use the -e option with ‘echo’ to print escaped characters. echo «=============» echo «VERTICAL TABS» echo -e «\v\v\v\v» # Prints 4 vertical tabs. echo «==============» echo «QUOTATION MARK» echo -e «\042″ # Prints » (quote, octal ASCII character 42). echo «==============» # The $’\X’ construct makes the -e option unnecessary. echo; echo «NEWLINE AND BEEP» echo $’\n’ # Newline. echo $’\a’ # Alert (beep). echo «===============» echo «QUOTATION MARKS» # Version 2 and later of Bash permits using the $’\nnn’ construct. # Note that in this case, ‘\nnn’ is an octal value. echo $’\t \042 \t’ # Quote («) framed by tabs. # It also works with hexadecimal values, in an $’\xhhh’ construct. echo $’\t \x22 \t’ # Quote («) framed by tabs. # Thank you, Greg Keraunen, for pointing this out. # Earlier Bash versions allowed ‘\x022’. echo «===============» echo # Assigning ASCII characters to a variable. # —————————————- quote=$’\042′ # » assigned to a variable. echo «$quote This is a quoted string, $quote and this lies outside the quotes.» echo # Concatenating ASCII chars in a variable. triple_underline=$’\137\137\137′ # 137 is octal ASCII code for ‘_’. echo «$triple_underline UNDERLINE $triple_underline» echo ABC=$’\101\102\103\010′ # 101, 102, 103 are octal A, B, C. echo $ABC echo; echo escape=$’\033′ # 033 is octal for escape. echo «\»escape\» echoes as $escape» # no visible output. echo; echo exit 0

See Example 34-1 for another example of the $’ ‘ string expansion construct.

gives the quote its literal meaning

echo «Hello» # Hello echo «\»Hello\», he said.» # «Hello», he said.

gives the dollar sign its literal meaning (variable name following \$ will not be referenced)

echo «\$variable01» # results in $variable01

gives the backslash its literal meaning

echo «\\» # Results in \ # Whereas . . . echo «\» # Invokes secondary prompt from the command line. # In a script, gives an error message.

The behavior of \ depends on whether it is itself escaped, quoted, or appearing within command substitution or a here document .

# Simple escaping and quoting echo \z # z echo \\z # \z echo ‘\z’ # \z echo ‘\\z’ # \\z echo «\z» # \z echo «\\z» # \z # Command substitution echo `echo \z` # z echo `echo \\z` # z echo `echo \\\z` # \z echo `echo \\\\z` # \z echo `echo \\\\\\z` # \z echo `echo \\\\\\\z` # \\z echo `echo «\z»` # \z echo `echo «\\z»` # \z # Here document cat

Читайте также:  Kernel shared memory linux

Elements of a string assigned to a variable may be escaped, but the escape character alone may not be assigned to a variable.

variable=\ echo «$variable» # Will not work — gives an error message: # test.sh: : command not found # A «naked» escape cannot safely be assigned to a variable. # # What actually happens here is that the «\» escapes the newline and #+ the effect is variable=echo «$variable» #+ invalid variable assignment variable=\ 23skidoo echo «$variable» # 23skidoo # This works, since the second line #+ is a valid variable assignment. variable=\ # \^ escape followed by space echo «$variable» # space variable=\\ echo «$variable» # \ variable=\\\ echo «$variable» # Will not work — gives an error message: # test.sh: \: command not found # # First escape escapes second one, but the third one is left «naked», #+ with same result as first instance, above. variable=\\\\ echo «$variable» # \\ # Second and fourth escapes escaped. # This is o.k.

Escaping a space can prevent word splitting in a command’s argument list.

file_list=»/bin/cat /bin/gzip /bin/more /usr/bin/less /usr/bin/emacs-20.7″ # List of files as argument(s) to a command. # Add two files to the list, and list all. ls -l /usr/X11R6/bin/xsetroot /sbin/dump $file_list echo «————————————————————————-» # What happens if we escape a couple of spaces? ls -l /usr/X11R6/bin/xsetroot\ /sbin/dump\ $file_list # Error: the first three files concatenated into a single argument to ‘ls -l’ # because the two escaped spaces prevent argument (word) splitting.

The escape also provides a means of writing a multi-line command. Normally, each separate line constitutes a different command, but an escape at the end of a line escapes the newline character , and the command sequence continues on to the next line.

(cd /source/directory && tar cf — . ) | \ (cd /dest/directory && tar xpvf -) # Repeating Alan Cox’s directory tree copy command, # but split into two lines for increased legibility. # As an alternative: tar cf — -C /source/directory . | tar xpvf — -C /dest/directory # See note below. # (Thanks, St�phane Chazelas.)

If a script line ends with a | , a pipe character, then a \ , an escape, is not strictly necessary. It is, however, good programming practice to always escape the end of a line of code that continues to the following line.

echo «foo bar» #foo #bar echo echo ‘foo bar’ # No difference yet. #foo #bar echo echo foo\ bar # Newline escaped. #foobar echo echo «foo\ bar» # Same here, as \ still interpreted as escape within weak quotes. #foobar echo echo ‘foo\ bar’ # Escape character \ taken literally because of strong quoting. #foo\ #bar # Examples suggested by St�phane Chazelas.

Источник

Escape in linux shell

Certain characters are significant to the shell; we have seen, for example, that the use of double quotes ( » ) characters affect how spaces and TAB characters are treated, for example:

$ echo Hello World Hello World $ echo "Hello World" Hello World

So how do we display: Hello «World» ?

The first and last » characters wrap the whole lot into one parameter passed to echo so that the spacing between the two words is kept as is. But the code:

Читайте также:  Linux не установлен telnet

Note that we lose the quotes entirely. This is because the first and second quotes mark off the Hello and following spaces; the second argument is an unquoted «World» and the third argument is the empty string; «».

is actually only one parameter (no spaces between the quoted parameters), and that you can test this by replacing the echo command with (for example) ls .

Most characters ( * , ‘ , etc) are not interpreted (ie, they are taken literally) by means of placing them in double quotes («»). They are taken as is and passed on to the command being called. An example using the asterisk ( * ) goes:

$ echo * case.shtml escape.shtml first.shtml functions.shtml hints.shtml index.shtml ip-primer.txt raid1+0.txt $ echo *txt ip-primer.txt raid1+0.txt $ echo "*" * $ echo "*txt" *txt

In the first example, * is expanded to mean all files in the current directory.
In the second example, *txt means all files ending in txt .
In the third, we put the * in double quotes, and it is interpreted literally.
In the fourth example, the same applies, but we have appended txt to the string.

However, » , $ , ` , and \ are still interpreted by the shell, even when they’re in double quotes.
The backslash ( \ ) character is used to mark these special characters so that they are not interpreted by the shell, but passed on to the command being run (for example, echo ).
So to output the string: (Assuming that the value of $X is 5):

A quote is ", backslash is \, backtick is `. A few spaces are and dollar is $. $X is 5.
$ echo "A quote is \", backslash is \\, backtick is \`." A quote is ", backslash is \, backtick is `. $ echo "A few spaces are and dollar is \$. \$X is $." A few spaces are and dollar is $. $X is 5.

We have seen why the » is special for preserving spacing. Dollar ( $ ) is special because it marks a variable, so $X is replaced by the shell with the contents of the variable X . Backslash ( \ ) is special because it is itself used to mark other characters off; we need the following options for a complete shell:

$ echo "This is \\ a backslash" This is \ a backslash $ echo "This is \" a quote and this is \\ a backslash" This is " a quote and this is \ a backslash

So backslash itself must be escaped to show that it is to be taken literally. The other special character, the backtick, is discussed later in Chapter 12, External Programs.

Источник

5 essential escape characters in Linux Shell

Escape sequences are an invaluable tool for entering special characters and events that cannot be found on a standard keyboard. BASH shell is particularly rich in escape sequences, some of which are more useful than others. In this article, we will discuss five of the most essential escape sequences, which can be highly beneficial to know.

Whether it’s for making a script more efficient, debugging, or just for making life easier, these five escape sequences are essential tools to have in any programmer’s toolkit. From the simple yet helpful \t tab character, to the complex \u unicode character, these escape sequences provide a wide range of capabilities to help make your work easier.

Читайте также:  Администрирование linux через web

Backspace

A backspace character can be entered as part of a command to trigger once the command executes. For example, running the command:

The expected output would be ab , but you’ll see b instead. Although technically, the shell did output ab (you can confirm that by appending | wc -m to the command), but part of the total output was the \b backspace event. The backspace removed a before outputting b , so the viewable output is just b .

Newline

A newline character is a signal for your shell to go to column 0 of the next line. This is essential when using a command like printf , which doesn’t assume that you want a newline added to the end of your output, the way echo does. For example, compare the difference between a printf statement without the \n newline character and one with it:

$ printf "%03d.txt" 25 025.txt$ $ printf "%03d.txt\n" 2 002.txt 

Form feed

A \f form feed signal is like a newline character, but without the imperative to return to column 0. For instance, the printf command using a form feed instead of a newline:

Your shell prompt is on the next line, but not at the start of the line.

Tab

There are two tab escape sequences: the \t horizontal tab and the \v vertical tab. The horizontal tab is exactly what you’d expect.

The vertical tab is, in theory, the same principle but in vertical space. On most consoles, though, the vertical spacing of a line isn’t variable, so it usually ends up looking a lot like a form feed:

Unicode

The Unicode standard has many available characters, and your keyboard only has about 100 keys. One way to enter special characters on Linux is to use the Unicode escape sequence. You can start this escape sequence with \u followed by a hexadecimal value. You can find many Unicode values in the file /usr/share/X11/locale/en_US.UTF-8/Compose , or you can look at the Unicode specification at https://www.unicode.org/charts/ .

This can be a useful trick for entering common symbols like Pi (the ratio of a circle’s circumference to its diameter):

There are lots of other symbols and characters, too.

$ echo '\u2709' ✉ $ echo '\u2601' ☁ $ echo '\u231B' ⌛ 

There’s Braille notation, musical notation, alphabets, electrical symbols, mathematical symbols, emoji, game symbols, and much more. In fact, there are so many available symbols that sometimes you need the \U (note the capital letter) Unicode escape sequence to access Unicode in the high ranges. For instance, this rocket emoji only appears with the \U escape sequence:

Muvon co-founder. 19+ years as a Software Engineer and startuper. I use 80/20 rule to reach maximum results. I will help you to reach new highs in development and life.

Сommunity is filled with like-minded individuals who are passionate about learning and growing as engineers.

Источник

Оцените статью
Adblock
detector