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 generally used to execute commands in a certain sequence, defined by the placement of operators between the commands. This is incredibly useful in automating tasks.

Let’s learn about a few common-chaining operators on Linux that can help you complete tasks faster and with lesser user intervals.

1. The Ampersand Operator (&)

Often when you open a program or execute a command from the shell, you have to either wait till the command terminates or manually exit out of the program before you can continue using the shell. Here’s where the ampersand operator (&) comes into play.

By appending the ampersand operator to any command, you dictate the shell to execute that Linux command in the background so that you can continue using the shell untethered.

gedit &

Usually, if you run gedit from the terminal, you will be unable to use the terminal unless you close the text editor. But, by appending the ampersand operator, you can make it run in the background and continue to use 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 defined, sequential order. Order your commands and separate them by semicolons.

pwd ; mkdir test ; cd test ; touch file

The above syntax dictates the shell to execute each command one after the other. Note that the shell does not check if each command terminates successfully. As soon as the shell receives a return code, it moves on to executing the next command.

3. The OR Operator (||)

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

bad_command || ls

In this example syntax, bad_command is a false command that will fail to execute and since it fails, the command succeeding the OR operator, which is the ls command, will execute successfully.

4. The Pipe Operator (|)

The pipe operator directs the output of the preceding command as input to the succeeding command. It is most commonly 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 functions in similar ways to the semicolon operator except, unlike the semicolon operator, AND operator will execute commands only if the preceding command was successfully executed.

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

In this example syntax, the shell will successfully execute all the commands up until bad_command. However, since bad_command fails to run, the shell will return an error and skip the ls command.

6. The NOT Operator (!)

The NOT operator works in similar ways to an except statement in programming. For instance, if you want to perform an operation on a large number of files in a directory but want to exclude a few based on some parameter, then you can use the NOT operator by passing the parameter after the NOT character (!).

rm -r !(*.txt)

This sample command will recursively remove all files in a directory except for files that have a “.txt” extension.

7. The Precedence Operator ((..))

The commands following the AND and OR operators depend on the exit code of the preceding command. These operators are binary and only evaluate the two commands that come before and after them.

So when working with multiple operators, it’s important to set groups and precedence 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 0 to ensure the successful execution of the last command. This example requires that both commands in the first set () exit with 0 for the second set () to run.

8. The Combination Operator ({..})

As the name hints, the combination operator is used to group commands. Whichever commands you want to group you can place them inside curly brackets, and they will be executed depending upon the exit code of the first command.

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

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

9. Concatenation or the Escape Operator (\)

The concatenation or escape operator has two functions. You can either 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 make four directories named test0 through test2, and the second command will print the string separated by a new line.

10. The Redirection Operators (>, >>, <)

The redirection operators redirect output or input to a file either by re-writing the file or by appending to it. If you want to re-write a file, then you have to use the single angle bracket (>) syntax. If you want to append to a file, you’ll have to use the double angle bracket syntax (>>).

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

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

Speed Up Your Linux Terminal Workflow

While using special operators will significantly streamline task execution for you, there are multiple other ways to speed up your workflow.

One easy and worthwhile way to get more done in less time is to familiarize yourself with a few handy shell shortcuts. Getting used to these shortcuts will go a long way, and you will find yourself relying on them for as long as you use Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker