Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

Linux - Control Operators

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Every task you do on Linux can be done using commands. But what if you want to execute two different commands at the same time, or you want to get the exit code of the command. All of this can be done using control operators.

Linux Control Operators

This blog will explain what control operators are in Linux and how we can use control operators in Linux.

Control operators

Control Operators help us to do different things in Linux. If we want to write two different commands on the same line in Command Line, then we can use the control operator. 

We have different control operators in Linux that are used to do different things. 

Here is the list of different control operators.

Control Operators Description

Now, Let’s discuss all of these control operators in detail with some examples.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Semicolon (;)

A semicolon (;) is used to denote the separation between commands that can be placed on the same line. The shell will scan the line until it encounters a semicolon. 

All arguments that come before this semicolon will be treated as separate commands from those that come after the semicolon.

Both series will be carried out in order, with the shell waiting until each command has been completed before moving on to the next.

Example

Semicolon Example

As we can see in the above example, both of the commands are executed in sequential order. And we don’t have to write the commands on separate lines.

Ampersand (&)

The shell does not wait for a command to complete when a line ends with the ampersand &. The command runs in the background while you return to your shell prompt. When this command has completed running in the background, you will receive a notification.

Example

Ampersand example

Dollar question mark ($?)

The shell variable $? contains the previous command's exit code. Since you cannot provide $? a value, it is a shell argument and not a variable.

Example

Dollar question mark example

There are different exit codes for the different results of the command.

Double ampersand (&&)

&& will be interpreted by the shell as a logical AND. If you use &&, the second command is only carried out if the first one is successful (returns a zero exit status).

Example

double ampersand example

Double vertical bar (||)

Logical OR is represented by the ||. Only when the first command fails is the second command carried out (returns a non-zero exit status).

Example

Double Vertical Bar Example

Combining (&&) and (||)

You can use this logical AND and logical OR to write an if-then-else structure on the command line.

Example

Example

Pound sign (#)

The shell ignores everything that is written after the pound sign (#). This can be used to add a shell comment, but it has no bearing on how a command will run or how the shell will expand.

Example

Example

Escaping special characters (\)

The backslash character makes it possible to use control characters, but doing so without the shell understanding them is known as escaping characters.

Example

Example

End of line backslash

Lines that finish in a backslash are carried over to the next line. The shell does not interpret the newline character and will delay shell expansion and command line execution until it encounters a newline without a backslash.

Example

Example

Frequently asked questions

What is the exit code in Linux?

An exit code in Linux represents the result of the execution of a command or script. It ranges from 0 to 255. We can tell whether a process ran successfully or not by looking at the exit codes.

What are two types of Linux User Mode?

Command Line and GUI are the two types of Linux user modes.

What is the maximum length for a filename under Linux?

Under Linux, a filename can only be 255 bytes long.

Conclusion 

In this blog, we have discussed control operators in Linux with many examples, and we have also seen what all these control operators does.

If you think this blog has helped you enhance your knowledge about the above question, and if you want to learn more, check out our articles. 

🔥 Linux - Decision Making
 

🔥 Linux File System
 

🔥 Linux- Shell Loops
 

🔥 Linux Security

And many more on our Coding Ninjas Studio.

Visit our website to read more such blogs. Make sure that you enroll in the courses we provide, take mock tests, solve problems available, and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations. 

Please upvote our blog to help other ninjas grow.

Happy Learning!

Topics covered
1.
Introduction
2.
Control operators
3.
Semicolon (;)
4.
Ampersand (&)
5.
Dollar question mark ($?)
6.
Double ampersand (&&)
7.
Double vertical bar (||)
8.
Combining (&&) and (||)
9.
Pound sign (#)
10.
Escaping special characters (\)
11.
End of line backslash
12.
Frequently asked questions
12.1.
What is the exit code in Linux?
12.2.
What are two types of Linux User Mode?
12.3.
What is the maximum length for a filename under Linux?
13.
Conclusion