directory

Basic commands

variable

Create and run the script

conditions

cycle

function

Others (read, mktemp, trap)

Basic commands

The following command displays the Shell currently running.

$ echo $SHELL
/bin/bash
Copy the code

The following command displays all the shells currently installed on your Linux system.

$ cat /etc/shells
Copy the code

In the above two commands, $is the prompt for the command line environment, and the user only needs to enter what follows the prompt.

Users can use the –version parameter of the bash command or the environment variable

$ bash --version
echo "$BASH_VERSION"
Copy the code

The echo command

The echo command is used to print a line of text on the screen. You can print the parameters of the command as they are in quotation marks.

$ echo "Hello\nWorld"
Hello\nWorld
​
$ echo -e "Hello\nWorld"
Hello
World
Copy the code

Common parameters

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

$ command foo bar
Copy the code

Configuration parameters

Some parameters are common parameters, while others are configuration items of the command. These configuration items usually start with a conjunction. For example, -r and –reverse are.

ls -r /home
ls --reverse /home
Copy the code

For example, -r is the short form, and –reverse is the long form. They have the same functions. The short form is easy to enter manually, and the long form is usually used in scripts.

Command combination

Command combination operator can help control the secondary relationship between multiple commands, there are three kinds of circumstances, the first kind of circumstance is &&, the second is | |, the third is; .

Command1 && Command2
Copy the code

The above command means that if Command1 is successful, Command2 continues to be run.

Command1 || Command2
Copy the code

If Command1 succeeds, Command2 is not executed.

Command1; Command2
Copy the code

The Command Command is not executed, regardless of whether Command1 is successful or not.

Special character extension

If the argument contains special characters, the Shell first expands it to its full contents before continuing with the command invocation.

The relationship between pattern extensions and regular expressions is that pattern extensions predate regular expressions and can be regarded as original regular expressions. It is less powerful and flexible than regular, but has the advantage of simplicity and convenience.

  • ~ Character extension
$ echo ~
/home/me
Copy the code

The tilde indicates the user directory.

  • ? Character extension
$ ls ? .txt a.txt b.txtCopy the code

The question mark represents any single character, such as the above command to obtain a TXT file containing only one character (any character).

  • * Character extension
$ ls *.txt
a.txt b.txt ab.txt
Copy the code

The asterisk represents any zero or more characters, such as the bright above to get any.txt file.

Note that ls * does not match hidden files (with. Ls does not output hidden files. To match a hidden file, write.

Echo **/*.txt # echo **/*.txt # echo **/*.txt # echo **/*.txt # echo **/*.txt #Copy the code
  • [] Character extension
Ls [^ab]*.txt # All files in the current directory which start with a, b and end with.txt ls [^ab]*.txt # All files in the current directory which do not start with a, b and end with.txtCopy the code

Quotes and escapes

$ echo "*"
*
Copy the code

There is no file name extension inside the double quotes. For example, the wildcard * is a special character. When placed inside the double quotes, it becomes a normal character. However, three special characters are excluded: the dollar sign ($), the backquote (‘), and the backslash (), which still have a special meaning within double quotes and are automatically extended by Bash.

The dollar sign ($) is expanded to the environment variable content

$ echo "${SHELL}"
/bin/bash
Copy the code

The backquote (‘) expands to the execution result of the command

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

The backslash () expands to an escape character

$ echo "Hello\nWorld"
Hello
World
Copy the code

The pipe

Add a | between two commands, is established between the two commands pipeline, pipeline is mainly used for processing a command output is the next command input this kind of situation.

Such as statistical ~ /. We hope bashrc file number of the character, we need two commands, the first command is used to read out the contents of the file (the cat ~ /. Bashrc), the second command is used to statistics the number of characters (wc – c), in order to finish the work, we can add a pipe between the two commands (|), combine them, Accomplish target work.

cat ~/.bashrc | wc -c
Copy the code

For example, if we want to see if there is a Java virtual Machine running in the process, we can use the following command.

ps -ef | grep java
Copy the code