Comprehensive Guide to Linux Command Delimiters: A Veteran’s Insights on Command Line Mastery

As a Linux operations engineer, mastering command delimiters is a key skill to enhance work efficiency.

This article will detail commonly used command delimiters in the CentOS7 system and their usage techniques, helping you operate the command line with ease.

01

Basic Delimiters

Semicolon (;) and Logical AND (&&)

1. Semicolon (;) – Execute sequentially without conditions

The semicolon is the simplest command delimiter, allowing you to execute multiple commands in the same line, regardless of whether the previous command was successful; the subsequent commands will continue to execute.Execute three commands in sequence, independent of each other

clear; ls -l; date

This format is particularly suitable for command combinations that have no dependencies, such as clearing the screen, listing the directory, and finally displaying the time.2. Logical AND (&&) – Intelligent conditional execution

The && symbol is more “intelligent”; it indicates that the subsequent command will only execute if the previous command was successful (return value is 0).Only enter and list contents if the directory exists

-d /tmp  && cd /tmp && ls

This format is particularly useful when installing software: Only install new software if yum update is successful

yum update -y && yum install -y nginx

In operations work, I often use && to ensure the sequential execution of critical steps, avoiding erroneous operations.

02

Logical OR (||)

Fallback options on failure

Double vertical bars || indicate that the subsequent command will only execute if the previous command fails, equivalent to a logical “otherwise”.Try to open a file with vim, if it fails, use nano

vim /etc/nginx.conf || nano /etc/nginx.conf

In practical operations, I often use it for error handling: If stopping the service fails, forcibly kill the process

systemctl stop nginx || pkill -9 nginx

More complex combinations: Only create if the directory does not exist, and only enter if creation is successful

 -d /backup  || mkdir /backup && cd /backup

This combination allows for intelligent conditional judgment, reducing manual intervention.

03

Pipe (|)

Data transfer between commands

The pipe | is one of the most powerful features of Linux, allowing the output of the previous command to be used as the input for the next command.View processes and filter for nginx

ps aux | grep nginx

Practical cases in operations: View large files and sort them

du -sh /* | sort -hr

Count the occurrences of 404 errors in logs

cat access.log | grep '404' | wc -l

Pipes can connect multiple commands indefinitely: Complex log analysis processes

cat /var/log/messages | grep 'error' | awk '{print $5}' |sort | uniq -c | sort -nr

Pipes transmit text data streams, not the files themselves.

04

Background execution (&)

Free up the terminal

Adding the & symbol at the end of a command allows it to run in the background, freeing the current terminal.Background execution of large file compression

tar -zcf backup.tar.gz /var/log &

Common techniques in operations, background execute scripts and log output

nohup ./deploy.sh > deploy.log 2>&1 &

Background jobs will terminate when the terminal closes; long-running tasks should be used with nohup.

05

Combined usage

Build powerful command chains

True experts mix these symbols to construct complex command logic. Try to stop the service, if it fails then kill it, and finally restart regardless of the result

systemctl stop nginx || pkill -9 nginx && systemctl start nginx

Backup the database: compress if successful, send an email if failed

mysqldump -uroot -p dbname > backup.sql && gzip backup.sql || mail -s "Backup Failed" [email protected]

06

Parentheses grouping

Organize complex logic

Using parentheses allows you to group multiple commands into a single unit, especially suitable for complex conditional judgments. Only operate if the directory exists and is writable

 -d /data  && (cd /data && tar -xf backup.tar)

Typical applications in operations: Conditional compilation installation

./configure && (make && make install) || echo "Compilation Failed"

Commands within parentheses will execute in a subshell, and variable modifications will not affect the parent shell.

07

Path-related symbols

Quick navigation

CentOS7 also has some special path delimiters:1. Single dot (.) : Current directory

Execute a script in the current directory

./script.sh   

2. Double dot (..) : Parent directory

Enter the sibling logs directory

cd ../logs   

3. Tilde (~) : User home directory

Copy to home backup directory

  cp file.txt ~/backup/ 

4. Hyphen (-) : Return to the last directory

 cd /var/log cd /tmp cd -   # Return to /var/log

These symbols greatly simplify path operations.

08

Quoting system

Fine control of parameters

Different quotes affect how commands parse delimiters1. Backticks “ ` “ or $() : Execute command

echo "Current time: `date`"

2. Single quotes ” : Literal interpretation

Output $HOME instead of the variable value

echo '$HOME'     

3. Double quotes “” : Allow variable expansion

Output variable value

echo "Home directory: $HOME"   

Correctly using quotes can avoid many parameter parsing issues.

09

Practical tips and precautions

Priority rules: && and || have higher precedence than ;

cmd1 && cmd2 || cmd3; cmd4

This is equivalent to

(cmd1 && cmd2) || cmd3; cmd4

Return value check: Each command has a return value after execution, 0 indicates success

command

Check the return value of the last command

echo $?   

Long command line breaks: Use \ to split long commands into multiple lines

 ls -l \ --sort=size \ --reverse \ /var/log

Avoid excessive combinations: overly complex command chains are difficult to debug; split into scripts when necessary

10

Conclusion

Mastering Linux command delimiters is an essential path to becoming an efficient operations engineer.

Remember these core points:

1. ; for simple sequential execution

2. && for continuing execution on success

3. || for fallback on failure

4. | for data transfer between commands

5. & for background execution

6. () for command grouping

Reasonably combining these symbols can make your command line operations smooth and efficient. It is recommended to start practicing with simple combinations and gradually build more complex command chains. Clear logic is more important than flashy techniques! I hope this article helps you improve your Linux operations efficiency. If you have any questions, feel free to leave a comment for discussion.

Here is a paid column on Linux commands, containing 100 commonly used Linux commands, including their usage, and also includes 73 operational issues and solutions I encountered at work.

If you need a partner, you can directly scan the QR code below. After purchasing, you can read and learn on WeChat. Currently, this column only costs 10 yuan. For the price of a cup of milk tea, you can take the column content home, which is quite cost-effective.

Comprehensive Guide to Linux Command Delimiters: A Veteran's Insights on Command Line Mastery

Welcome to scan and follow the public account below, ask Linux-related questions in the background, and experience intelligent replies.

Leave a Comment