Bash basic syntax

This chapter introduces the most basic syntax of Bash.

echo command

Since the following examples will use the echo command a lot, here is the first introduction to this command.

The function of the echo command is to output a line of text on the screen, and the parameters of the command can be output as they are.

$ echo hello world
hello world

In the above example, the parameter of echo is hello world, which can be output as it is.

If you want to output multiple lines of text, include line breaks. At this time, you need to put multiple lines of text in quotation marks.

$ echo "<HTML>
    <HEAD>
          <TITLE>Page Title</TITLE>
    </HEAD>
    <BODY>
          Page body.
    </BODY>
</HTML>"

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

-n parameter

By default, there will be a carriage return at the end of the text output by echo. The -n parameter can cancel the carriage return at the end, making the next prompt immediately follow the output.

$ echo -n hello world
hello world$

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

$ echo a;echo b
a
b

$ echo -na;echo b
ab

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

-e parameter

The -e parameter will interpret the special characters (such as the newline character \n) inside the quotes (double quotes and single quotes). If you do not use the -e parameter, that is, by default, quotation marks will make special characters into ordinary characters, and echo will not interpret them and output them as they are.

$ echo "Hello\nWorld"
Hello\nWorld

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

# The case of single quotes
$ echo -e'Hello\nWorld'
Hello
World

In the above code, the -e parameter makes \n interpreted as a newline character, resulting in a newline in the output content.

Command format

In the command line environment, various operations are performed mainly by using Shell commands. Shell commands are basically in the following format.

$ command [arg1 ... [argN ]]

In the above code, command is a specific command or an executable file, and arg1 ... argN are parameters passed to the command. They are optional.

$ ls -l

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

Some parameters are command configuration items, and these configuration items generally start with a connecting line, such as the above -l. The same configuration item often has long and short forms. For example, -l is the short form, and --list is the long form, and their functions are exactly the same. The short form is convenient for manual input, and the long form is generally used in scripts, which is more readable and helps explain its meaning.

# Short form
$ ls -r

# Long form
$ ls --reverse

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

A single command of Bash is generally one line, and the user presses the Enter key to start execution. Some commands are relatively long and written in multiple lines will facilitate reading and editing. At this time, you can add a backslash at the end of each line, and Bash will put the next line together with the current line for explanation.

$ echo foo bar

# Equivalent to
$ echo foo \
bar

Space

Bash uses spaces (or Tab keys) to distinguish different parameters.

$ command foo bar

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

If there are multiple spaces between the parameters, Bash will automatically ignore the extra spaces.

$ echo this is a test
this is a test

In the above command, there are multiple spaces between a and test, and Bash will ignore the extra spaces.

Semicolon

The semicolon (;) is the terminator of a command, which allows multiple commands to be placed in one line. After the previous command is executed, the second command is executed.

$ clear; ls

In the above example, Bash executes the clear command first, and then executes the ls command after the execution is complete.

Note that when using a semicolon, the second command is always executed after the first command, regardless of whether the first command is executed successfully or failed.

Command combination && and ||

In addition to the semicolon, Bash also provides two command combinators && and ||, allowing better control of the subsequent relationship between multiple commands.

Command1 && Command2

The above command means that if the Command1 command runs successfully, continue to run the Command2 command.

Command1 || Command2

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

Here are some examples.

$ cat filelist.txt; ls -l filelist.txt

In the above example, as long as the execution of the cat command ends, regardless of success or failure, the ls command will continue to be executed.

$ cat filelist.txt && ls -l filelist.txt

In the above example, the ls command will continue to be executed only if the cat command is successfully executed. If the execution of cat fails (for example, the file flielist.txt does not exist), then the ls command will not be executed.

$ mkdir foo || mkdir bar

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

type command

Bash itself has many built-in commands, and it can also execute external programs. How do you know whether 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)

In the above code, 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

If you want to view all the definitions of a command, you can use the -a parameter of the type command.

$ type -a echo
echo is shell builtin
echo is /usr/bin/echo
echo is /bin/echo

The above code indicates that the echo command is a built-in command and also has a corresponding external program.

The -t parameter of the type command can return the type of a command: alias, keyword, function, builtin and file.

$ type -t bash
file
$ type -t if
keyword

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

hot key

Bash provides many shortcut keys, which can greatly facilitate the operation. The following are some of the most commonly used shortcut keys. For a complete introduction, please refer to the chapter "Line Operations".

  • Ctrl + L: Clear the screen and move the current line to the top of the page.
  • Ctrl + C: Abort the currently executing command.
  • Shift + PageUp: Scroll up.
  • Shift + PageDown: Scroll down.
  • Ctrl + U: Delete from the cursor position to the beginning of the line.
  • Ctrl + K: Delete from the cursor position to the end of the line.
  • Ctrl + D: Close the Shell session.
  • , : browse the history of executed commands.

In addition to the above shortcut keys, Bash also has an auto-completion function. When the command is halfway through, you can press the Tab key, and Bash will automatically complete the rest. For example, enter pw, and then press the Tab key, Bash will automatically add d.

In addition to the automatic completion of commands, Bash also supports automatic completion of paths. Sometimes, you need to enter a very long path. At this time, you only need to enter the first part, and then press the Tab key, and the latter part will be automatically completed. If there are multiple possible choices, press the Tab key twice and Bash will display all the options for you to choose.