directory

 

The echo command

The -n parameter

– e parameters

The command format

The blank space

A semicolon

Command combination operators && and | |

Type the command

shortcuts


The echo command

Since the echo command will be heavily used in the following examples, I’ll introduce it here.

The echo command outputs a line of text on the screen. You can output the parameters of the command as they are.

$ echo hello world
hello world
Copy the code

In the example above, echo takes hello World and can be printed as is.

If you want to output multiple lines of text, include newlines. You need to enclose multiple lines of text in quotes.

$ echo "  Page Title   Page body.  "
Copy the code

In the example above, echo can output multiple lines of text as is.

 

-nparameter

By default, echo output has a carriage return at the end of the text. The -n argument cancels the carriage return at the end so that the next prompt follows the output.

$ echo -n hello world
hello world$
Copy the code

In the example above, world is immediately followed by the $prompt on the next line.

$ echo a;echo b
a
b

$ echo -n a;echo b
ab
Copy the code

In the example above, the -n argument allows the output of the two echo commands to be joined together and appear on the same line.

 

-eparameter

The -e argument interprets special characters (such as newline \n) inside quotation marks (double and single). If you do not use the -e argument, by default, quotes turn special characters into normal characters, and echo does not interpret them and outputs them as is.

$ echo "Hello\nWorld"
Hello\nWorld

# double quotes
$ echo -e "Hello\nWorld"
Hello
World

# single quotes
$ echo -e 'Hello\nWorld'
Hello
World
Copy the code

In the code above, the -e argument causes \n to be interpreted as a newline character, resulting in a newline in the output.

 

The command format

In the cli environment, Shell commands are used to perform various operations. Shell commands are basically in the following format.

$ command [ arg1 ... [ argN ]]
Copy the code

In the above code, command is a concrete command or an executable file, arg1… ArgN is the arguments passed to the command, and they are optional.

$ ls -l
Copy the code

In the above command, ls is the command and -l is the argument.

Some parameters are configuration items of a command. These configuration items usually start with a conjunction line, such as -l above. A configuration item can be either long or short. For example, -lis short and –list is long. They have the same functions. The short form is easy to enter manually, while the long form is usually used in scripts, which is more readable and helps explain its own meaning.

# short form
$ ls -r

# long form
$ ls --reverse
Copy the code

In the above command, -r is the short form and –reverse is the long form, which has exactly the same effect. The former is easy to input and the latter to understand.

A Bash command is typically a single line that is executed when the user presses the Enter key. Some commands are long enough to be read and edited on multiple lines, so you can add a backslash at the end of each line, and Bash will interpret the next line alongside the current one.

$ echo foo bar

# is equal to
$ echo foo \
bar
Copy the code

 

The blank space

Bash uses the space (or Tab key) to distinguish between different arguments.

$ command foo bar
Copy the code

In the command above, there is a space between foo and bar, so Bash considers them to be two arguments.

If there are multiple Spaces between arguments, Bash automatically ignores the extra Spaces.

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

In the command above, there are multiple Spaces between a and test, and Bash ignores any extra Spaces.

 

A semicolon

Semicolon (;) Is the end of a command, so that multiple commands can be placed on one line. After the execution of the previous command is complete, the second command can be executed.

$ clear; ls
Copy the code

In the above example, Bash executes the clear command first, and then the ls command.

Note that with a semicolon, the second command always follows the first, regardless of whether the first command succeeds or fails.

 

Command combination&&and||

In addition to the semicolon, Bash provides two command combination operators && and | | to allow better control of the secondary relationship between multiple commands.

Command1 && Command2
Copy the code

The command above means that if Command1 is running successfully, the Command2 command continues.

Command1 || Command2
Copy the code

The command above means that if Command1 fails to run, the Command2 command continues.

Here are some examples.

$ cat filelist.txt ; ls -l filelist.txt
Copy the code

In the preceding example, the ls command continues to be executed after the cat command is executed, regardless of success or failure.

$ cat filelist.txt && ls -l filelist.txt
Copy the code

In the preceding example, the ls command is executed only after the cat command is successfully executed. If cat fails (for example, there is no file flielist.txt), the ls command will not execute.

$ mkdir foo || mkdir bar
Copy the code

In the example above, the mkdir bar command is executed only if the mkdir foo command fails (for example, the foo directory already exists). If the mkdir foo command is successful, the bar directory will not be created.

 

Type the command

Bash itself comes with many built-in commands, and you can also execute external programs. How do you know if a command is a built-in command or an external program?

The type command is used to determine the source of the command.

$ type echo
echo is a shell builtin
$ type ls
ls is hashed (/bin/ls)
Copy the code

In the code above, the type command tells us that echo is an internal command and ls is an external program (/bin/ls).

The type command itself is also a built-in command.

$ type type
type is a shell builtin
Copy the code

To see all the definitions of a command, use the -a parameter of the type command.

$ type -a echo
echo is shell builtin
echo is /usr/bin/echo
echo is /bin/echo
Copy the code

The above code shows that the echo command is both a built-in command and an external program.

The -t argument to the type command, which returns the type of a command: alias, keyword, function, builtin, and file.

$ type -t bash
file
$ type -t if
keyword
Copy the code

In the example above, bash is the file and if is the keyword.

 

shortcuts

Bash provides a number of keyboard shortcuts that make it much easier to use. Here are some of the most commonly used shortcuts. See the row Operations chapter for a complete description.

  • Ctrl + L: Clears the screen and moves the current line to the top of the page.
  • Ctrl + C: Terminates the command being executed.
  • Shift + PageUp: Scroll up.
  • Shift + PageDown: Scroll down.
  • Ctrl + U: Deletes from the cursor position to the beginning of the line.
  • Ctrl + K: Deletes from cursor position to end of line.
  • Ctrl + D: Closes the Shell session.
  • write.left: Displays the history of executed commands.

In addition to the shortcut keys above, Bash also has auto-completion. Halfway through typing a command, you can press the Tab key and Bash will do the rest automatically. For example, type PW and press Tab, and Bash automatically fills in d.

In addition to command auto-completion, Bash also supports path auto-completion. Sometimes, if you need to enter a long path, you just need to enter the first part and press Tab to automatically complete the next part. If there are multiple possible choices, press Tab twice and Bash will show you all the options for you to choose from.

 

The next section covers mode extensions to the Bash script tutorial