Linux command line, controlling command execution: the "&&", "||", ";" and "&" operators
Greetings!

Today we’re breaking down such powerful Linux command line tools as command execution control operators.

Previous posts in the series

If you’re just starting to learn Linux, I strongly recommend getting familiar with the previous materials:

  1. Linux command line, introduction: command types, plain text, file system, shell prompt
  2. Linux command line, navigating the system and viewing directories: pwd, ls, cd commands
  3. Linux command line, output and reading content: echo, cat, less commands
  4. Linux command line, working with files: touch, mkdir, cp, mv, rm commands
  5. Linux command line, file links: the ln command
  6. Linux command line, input and output redirection: the “>”, “<”, “|” operators

Introduction

Command line operators in Linux provide the ability to control the execution of commands and programs. The operators &&, ||, ; and & allow you to control the order of command execution and run them sequentially or in parallel.

Usage examples

The && operator (logical AND)

Example 1:

BASH
mkdir mydir && touch mydir/myfile.txt && ls mydir
Click to expand and view more

In this example, using the && operator, the directory “mydir” is created, then the touch command creates an empty file “myfile.txt” inside the “mydir” directory. Then the ls command outputs the content of the “mydir” directory.

Example 2:

BASH
ls mydir/

cd mydir/ && touch file1.txt && cp file1.txt file2.txt && rm file1.txt
Click to expand and view more

In this example, using the && operator, we change into the previously created “mydir” directory, then the file “file1.txt” is copied to the file “file2.txt”. If the copy operation is successful, the rm command deletes the file “file1.txt”.

The || operator (logical OR):

Example 1:

BASH
cat file.txt || echo "Файл не найден"

touch file.txt && echo "Hello R4ven" > file.txt

cat file.txt || echo "Файл не найден"
Click to expand and view more

In this example, the cat command tries to output the content of the file “file.txt”. If the file doesn’t exist, the || operator outputs the message “Файл не найден” (File not found). Then a file “file.txt” is created with the content “Hello R4ven” and the previous command is run again — the result of execution changed, and the block after || didn’t run.

Example 2:

BASH
ls images/ || mkdir images
Click to expand and view more

In this example, the || operator checks for the presence of the “images” directory. If the directory doesn’t exist, the mkdir command creates the “images” directory.

The ; operator (semicolon):

Example:

BASH
touch file1.txt ; cp -v file1.txt directory/ ; rm -v file1.txt
Click to expand and view more

In this example, using the ; operator, three commands are run sequentially. First, the touch command creates an empty file “file1.txt”. Then, using the cp command, the file “file1.txt” is copied to the “directory” folder, which doesn’t exist, resulting in an error. But despite this, the last command still runs: the rm command deletes the file “file1.txt”.

The & operator (ampersand):

Example:

BASH
sleep 1000 & echo "Процесс запущен в фоновом режиме"
Click to expand and view more

In this example, the sleep 1000 command is launched in the background using the & operator. The sleep 1000 command suspends execution for 1000 seconds. At the same time, after launching the command, the message “Процесс запущен в фоновом режиме” (Process launched in background mode) is output, allowing you to continue working in the command line.

After sending the process to background execution, the terminal outputs the process number in the local shell jobs id — [1] and the number (PID — process id) in the overall system list — 6034.

You can view the list of background processes for the current shell session with the jobs command, and the list of system processes with the ps command:

BASH
jobs

ps
Click to expand and view more

To bring a process from background mode to active mode, the fg (foreground) command is used, specifying the jobs id:

BASH
fg 1
Click to expand and view more

To terminate the sleep process, send the interrupt command with Ctrl+C.

I’ll cover working with processes in Linux in more detail in a separate post in the future.

When can command execution control operators come in handy?

Command line operators are widely used in task automation and scripting:

Afterword

Today we got familiar with the command line operators &&, ||, ; and &, which provide flexible capabilities for controlling the order of execution of commands and programs in Linux. Proper use of these operators allows you to automate tasks, handle errors, and improve efficiency when working with the command line.

Thank you for your attention. Good luck)

Useful sources

Copyright Notice

Author: Ivan Cherniy

Link: https://r4ven.me/en/linux/komandnaya-stroka-linux-kontrol-vypolneniya-komand/

License: CC BY-NC-SA 4.0

Blog materials may be used with attribution to the author and source, for non-commercial purposes, and under the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut