directory

 

escape

Single quotes

Double quotation marks

Here document

Here a string


escape

Certain characters have special meanings within Bash (such as $, &, and *).

$ echo $date

$
Copy the code

In the example above, printing $date will have no result because $is a special character.

If you want to print these special characters as is, you must precede them with a backslash to make them normal. This is called an escape.

$ echo \$date
$date
Copy the code

In the preceding command, only the special character $is preceded by a backslash to output as is.

The backslash itself is also a special character. If you want to print the backslash as is, you need to escape it by using two consecutive backslashes (\\).

$ echo\ \ \Copy the code

The example above prints the backslash itself.

In addition to being used for escape, backslashes can also represent characters that are not printable.

  • \aBell ring:
  • \b: backspace
  • \n: a newline
  • \r: enter
  • \t: tabs

If you want to use these non-printable characters on the command line, you can put them in quotes and use the -e argument of the echo command.

$ echo a\tb
atb

$ echo -e "a\tb"
a        b
Copy the code

In the example above, the command line directly prints the unprintable character \t, which Bash does not interpret correctly. You must place them in quotes, and then use the -e argument of the echo command.

A newline character is a special character that indicates the end of a command, and once Bash receives this character, it interprets and executes the entered command. A newline character is preceded by a backslash escape, which makes the newline a normal character that Bash treats as a space, allowing you to write multiple lines of a single command.

$ mv \
/path/to/foo \
/path/to/bar

# is equal to
$ mv /path/to/foo /path/to/bar
Copy the code

In the example above, if a command is too long, you can rewrite it to multiple lines by using a backslash at the end of the line. This is a common multi-line command.

 

Single quotes

Bash allows strings to be quoted within single or double quotation marks.

Single quotation marks are used to preserve the literal meaning of characters. Special characters, such as asterisks (*), dollar signs ($), and backslashes (\), are changed to common characters within single quotation marks.

$ echo The '*'
*

$ echo '$USER'
$USER

$ echo '$((2 + 2))
$((2+2))

$ echo '$(echo foo)'
$(echo foo)
Copy the code

In the above command, single quotes invalidate Bash extensions, variable references, arithmetic operations, and subcommands. If single quotes are not used, they are automatically extended by Bash.

The backslash becomes a common character inside the single quotation marks. Therefore, if the single quotation marks are used, do not use escape. You need to add a dollar sign ($) before the outer single quotation marks and then escape the inner single quotation marks.

# is not correct
$ echo it$echo '$echo'it\'s'

# right
$ echo $'it\'s'
Copy the code

However, it makes more sense to use single quotes instead of double quotes.

$ echo "it's"
it's
Copy the code

 

Double quotation marks

Double quotation marks are looser than single quotation marks. Most special characters inside double quotation marks lose their special meanings and become common characters.

$ echo "*"
*
Copy the code

In the example above, the wildcard * is a special character. When placed in double quotation marks, it becomes a normal character and is printed as it is. This is important because it means that no filename extension is done inside the double quotes.

However, three special characters are excluded: the dollar sign ($), backquotes (‘), and backslashes (\). These three characters, in double quotes, still have special meaning and are automatically extended by Bash.

$ echo "$SHELL"
/bin/bash

$ echo "`date`"
Mon Jan 27 13:33:18 CST 2020
Copy the code

In the example above, the dollar sign ($) and the backquote (‘) retain special meanings within the double quotes. Dollar signs are used to refer to variables, and backquotes are used to execute subcommands.

$ echo "I'd say: \"hello! \ ""
I'd say: "hello!" $ echo "\\" \Copy the code

In the example above, backslashes retain special meaning in double quotes and are used for escape. So, you can use backslashes, insert double quotes within double quotes, or insert backslashes themselves.

The newline character loses its special meaning within the double quotation marks, and Bash no longer interprets it as the end of a command, just as a plain newline character. So you can use double quotes to type multiple lines of text on the command line.

$ echo "hello
world"
hello
world
Copy the code

In the above command, Bash would normally interpret a line break as the end of the command, but the line break loses its special function in double quotes and is used only for line breaks, so you can type multiple lines. The echo command outputs the newline character as it is and normally interprets it as a newline.

Another common use of double quotation marks is when the file name contains Spaces. Double quotation marks must be used to enclose the file name.

$ ls "two words.txt"
Copy the code

In the command above, two words.txt is a filename with Spaces, otherwise Bash would treat it as two files.

Double quotes hold extra Spaces as they are.

$ echo "this is a test"
this is a     test
Copy the code

The double quotation marks also serve to preserve the output format of the original command.

# single line output
$ echo$(CAL) January 2020 one two three four five six one two three three... 31# Raw format output
$ echo "$(cal)"January 2020 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31Copy the code

In the example above, if $(CAL) is not enclosed in double quotes, Echo prints all the results in a single line, discarding all the original formatting.

 

Here document

A Here document is a way of entering a multi-line string in the following format.

<< token
text
token
Copy the code

Its format is divided into a start tag (<< token) and an end tag (token). The start tag is the name of two less-than + Here documents, optionally followed by a newline character; The closing tag is the name of the Here document written on a single line at the top. If it is not the top, the closing tag has no effect. In between are the contents of a multi-line string.

Here is an example of output HTML code through a Here document.

$ cat << _EOF_    The title of your page    Your page content goes here.   _EOF_
Copy the code

Variable substitutions occur inside Here documents, backslash escapes are supported, but wildcard extensions are not, and double and single quotes lose their syntactic effect and become ordinary characters.

$ foo='hello world'
$ cat << _example_
$foo
"$foo"
'$foo'
_example_

hello world
"hello world"
'hello world'
Copy the code

In the example above, the variable $foo is replaced, but the double and single quotes are printed as is, indicating that they have lost their ability to reference.

If you don’t want variable substitution, you can put the opening tag of a Here document in single quotes.

$ foo='hello world'
$ cat << '_example_'
$foo
"$foo"
'$foo'
_example_

$foo
"$foo"
'$foo'
Copy the code

In the example above, the Here document’s opening tag (_example_) is enclosed in single quotes, invalidating the variable substitution.

The essence of a Here document is redirection, which outputs a string redirect to a command that contains the echo command.

$ command << token string token

# is equal to

$ echo string | command
Copy the code

In the code above, the Here document acts as a redirection of the echo command.

Therefore, the Here string is only suitable for commands that accept standard input as an argument, and not for other commands, such as echo commands that cannot use a Here document as an argument.

$ echo << _example_
hello
_example_
Copy the code

The above example will have no output because the Here document is invalid for the echo command.

In addition, Here documents cannot be used as values of variables, only as arguments to commands.

 

Here a string

A variation of the Here document is called the Here string, which is represented by three less-than signs (<<<).

<<< string
Copy the code

What it does is it passes a string to a command through standard input.

Some commands take a given argument directly, and the result is different from taking the argument through standard input. That’s why this syntax makes it easier to pass strings from standard input to commands, such as the cat command, which only accepts strings from standard input.

$ cat <<< 'hi there'
# is equal to
$ echo 'hi there' | cat
Copy the code

The first syntax, which uses the Here string, looks semantically better and more concise than the second.

$ md5sum <<< 'ddd'
# is equal to
$ echo 'ddd' | md5sum
Copy the code

In the example above, the md5sum command can only accept standard input as arguments, and cannot be directly placed after the command. The DDD in the md5sum DDD command will be interpreted as the file name. You can then use the Here string and pass the string to the md5sum command.

 

Variables for the Bash script tutorial in the next section