10 Linux Command Line Operators and What They Do

Command chaining operators are special characters used to write miniature shell scripts in the command line. They are typically used to execute commands in a certain order, defined by the placement of operators between commands. This is incredibly useful for automating tasks.

Let’s explore some common chaining operators on Linux that can help you complete tasks faster and with lower usage intervals.

1. The ampersand (&) operator

Often when you open a program or run a command from the shell, you must either wait for the command to complete or manually exit the program before you can continue using the shell. This is where the ampersand (&) operator comes into play.

By adding the ampersand operator to any command, you are telling the shell to run that Linux command in the background so you can continue to use the shell without a login.

gedit &

Usually, if you run gedit from the terminal, you won’t be able to use the terminal unless you close the text editor. But, by adding the ampersand operator, you can make it work in the background and continue using the shell immediately.

2. The semicolon (;) operator

The semicolon operator is an incredibly useful Linux chaining operator that you can use to execute commands in a set sequential order. Order your commands and separate them with semicolons.

pwd ; mkdir test ; cd test ; touch file

The above syntax tells the shell to execute each command one after the other. Note that the shell does not check whether each command completes successfully. As soon as the shell receives a return code, it proceeds to execute the next command.

3. The OR operator (||)

The OR operator will execute the command that follows only if the previous command fails, i.e. returns an exit code of 0. It works like an OR logic gate, which returns a value of 1 when the input is 0.

bad_command || ls

In this syntax example, wrong_command is a bogus command that will not run and since it fails, the command succeeding the OR operator, which is the ls command, will run successfully.

4. The pipe operator (|)

The pipe operator pipes the output of the previous command as input to the next command. It is most often used to filter data with the grep command.

cat test | grep -i "makeuseof"

This command sends the output of the cat command as input to the grep command, which then filters the output against a specified string.

5. The AND operator (&&)

This operator works similar to the semicolon operator except that unlike the semicolon operator, the AND operator will only execute commands if the previous command was executed successfully.

pwd && mkdir test && cd test && bad_command && ls

In this sample syntax, the shell will successfully execute all commands up to wrong_command. However, since bad_command is not running, the shell will return an error and ignore the ls command.

6. The NOT operator (!)

The NOT operator works the same way as an except statement in programming. For example, if you want to perform an operation on a large number of files in a directory but want to exclude a few based on certain parameters, you can use the NOT operator by passing the parameter after the NOT (!) character.

rm -r !(*.txt)

This example command will recursively delete all files in a directory, except files that have a “.SMS” extension.

7. The precedence operator ((..))

The commands that follow the AND and OR operators depend on the exit code of the previous command. These operators are binary and only evaluate the two commands that precede and follow them.

So, when working with multiple operators, it is important to set groups and priority to ensure that the execution sequence meets your expectations.

(ls *.txt > txt-files.list && cp *.tx ~) && (ls *.deb > deb-packages.list && cp *.deb ~) || echo "Precedence Test!"

In this sample syntax, both groups of commands must return an exit code of 0 to ensure successful execution of the last command. This example requires both commands in the first set() to end in 0 for the second set() to run.

8. The combination operator ({..})

As the name suggests, the combination operator is used to group commands together. Whatever commands you want to group together, you can enclose them in braces and they will be executed according to the exit code of the first command.

test -f /etc/passwd && {pwd ; date} && echo $0 ; echo "Hello"

The example syntax will test if the /etc/passwd file is present, display the current working directory, date, shell name, and echo “Hello”.

9. Concatenation or escape operator ()

The concatenation or escape operator has two functions. You can use it to concatenate two commands or as an escape character when working with strings in the shell.

mkdir test0 test1  test2
echo "Hello! from the
other side"

The first command will create four directories named test0 through test2, and the second command will print the string separated by a newline.

10. Redirection Operators (>, >>,

Redirection operators redirect output or input to a file by either rewriting the file or appending to it. If you want to overwrite a file, you must use the single bracket (>) syntax. If you want to append to a file, you will need to use the double bracket (>>) syntax.

echo "dsd" > test ; echo "bssss" >> test

In the example syntax, the first command will overwrite the “test” file with the provided string, but in the second command, the provided string will be appended to the test file.

Speed ​​up your Linux terminal workflow

Although using special operators greatly streamlines task execution for you, there are many other ways to speed up your workflow.

A simple and useful way to do more in less time is to familiarize yourself with some handy shell shortcuts. Getting used to these shortcuts will go a long way, and you’ll find yourself using them for as long as you use Linux.

Comments are closed.