The use of -e, &, and && in bash scripts
1.&和&&
There are several different uses for the ampersand symbol in Linux bash scripts. Here are some common ones:
Background run command:
When you add an & to the end of a command, it puts the command in the background for execution. This means that you can continue to execute other commands on the same terminal while the command is running. For example:
python script.py & The command above starts the script.py script in the background.
Logical operator:
&& can be used as a logical operator, but it is more common to use &&. && 'is used to join two commands so that the second command is executed only after the first command is successfully executed (i.e., the exit status is 0).
command1 &&command2 is not commonly used as a logical operator because it does not check the exit status of the previous command.
Bit operation:
In bash scripts, & can also be used as the "by bit and" operator in bit operations. For example:
((result=var1 & var2)) The above command computes the bit-sum results of var1 and var2 and assigns them to result.
Job Control:
In bash, & is also related to job control. You can use the jobs command to see the list of jobs running in the background, use the fg command to bring background jobs to the foreground, or use the bg command to continue running suspended jobs in the background.
jobs # lists job fg %1 # brings job number 1 to the foreground bg %1 # Continues job number 1 in the background
File descriptor:
In advanced scripting, & can also be used to redirect file descriptors. For example, 2>&1 redirects standard error (file descriptor 2) to standard output (file descriptor 1).
command > Output.txt 2>&1 The above command redirects both the standard output and standard error of the command to the 'output.txt' file.
The meaning of an & can vary greatly depending on the context. When writing the script, make sure you clearly know what each & does to avoid confusion and potential errors.
2.-e
In Linux bash scripts, -e is often used with the echo command or the test/[command, which have different meanings and purposes.
The -e option in the echo command:
When the -e option is used in the echo command, it enables echo to interpret characters escaped by backslashes \. For example:
echo -e "Line 1\nLine 2" The command above outputs two lines of text, with "\n" interpreted as a newline.
test/[-e option in the command:
In the test or its alias [command, -e is used to check whether a file or directory exists. If the specified file or directory exists, the expression returns true (exit status is 0). For example:
if [ -e /path/to/file ]; then echo "File exists." else echo "File does not exist." fi "Then echo" file exists." Else echo "file does not exist." fi" Then echo "file exists." Else echo" file does not exist.
Be sure to pay attention to the context when using -e, as its meaning is completely different in different commands.
In this article about bash script -e, & & & The use of the article is introduced to this, more related to bash-e, & & & Please search for previous articles of Script House or continue to browse the following related articles hope that you will support Script House in the future!
Related article
-
How to use the expect command to realize Shell automatic interaction is explained in detail
We can realize simple control flow functions through the Shell, such as: loop, judgment and so on. The following article mainly introduces the relevant information about the use of expect command to achieve Shell automated interaction, the article through the example code introduction is very detailed, the need for friends can refer to, below to take a look at it.2017-12-12 -
Simple example of linux shell output line feed
This article mainly introduces the relevant information about linux shell output newline, the article introduces very detailed through the example code, which has certain reference value for everyone's study or work, and the friends who need it can refer to it2023-02-02 -
Summary of 73 commands commonly used in the Linux shell
For friends who often use the linux system, the basic commonly used shell commands are indispensable, the following article summarizes the common commands of the Linux shell for everyone, friends in need can be referred to for reference, let's learn together.2017-01-01 -
linux NIC configuration details and examples
This article mainly introduces the linux network card configuration details and examples of the relevant information, the need for friends can refer to the next2017-05-05 -
Shell script FTP automatically upload and download files
This paper mainly introduces the Shell script to achieve FTP automatic upload and download files, the main content includes batch download script code, download a single file script code, login FTP file upload function, upload a single file script code, etc2023-08-08 -
How do I get a timestamp using date
This article mainly introduces how to use date to obtain time stamps, this article gives you a very detailed introduction, for everyone's study or work has a certain reference value, need friends can refer to2023-08-08 -
Several methods of string sorting in the Shell
Today, Xiaobian will share an article about several methods of string sorting in the Shell. Xiaobian feels that the content is very good, and now it is shared with you, which has a good reference value. Friends need to follow Xiaobian to see2019-03-03 -
This section describes the mkdir command in Linux
Linux mkdir command is mainly used to create a directory, you can also directly create a multi-layer directory, this article will introduce the Linux mkdir command method, interested friends follow the small series together to see it2019-04-04 -
Command aliases, command history, and command substitution methods in the Shell
This article mainly introduces the command alias in the Shell, command history and command replacement methods, Xiaobian feel very good, now share with you, but also give you a reference. Let's take a look2018-06-06
Latest comments