Categories
Linux Tutorial Series

Linux Tutorial Series – 188.1 – Logical operators usage in tests

Here is the video version, if you prefer it:

We talked about logical operators (&& (and) and || (or)) and got to understand how they work. You can also use them in tests.

If you use &&, then if the first test is not successful, its exit code is used for the if statement and if the first test is successful, then the exit code of the second test is used for the if statement. If you use ||, if the first test is not successful, the second test’s exit code is used as the exit code for the if statement and if the first test is successful, the first test’s exit code is used as the exit code for the if statement. (Ward, 2014)⁠

For example, you can write:

if [ “$1” = ‘Hi’ ] || [ “$1” = ‘Hello’ ]

if you were to test if the first argument is either “Hi” or “Hello”.

Think of it like this: && says “Both of the conditions have to be true” while || says “Only one of the conditions must be true”. This holds because only the exit value of 0 is used to indicate success. Re-read the second paragraph and make sure you understand this.

You can combine more than just two conditions with logical operators.

Hope you learned something useful!

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 258

Categories
Linux Tutorial Series

Linux Tutorial Series – 188 – Logical operators

Here is the video version, if you prefer it:

Let’s talk about logical operators outside of the context of shell scripts, at first.

If I run two commands like this:

touch someFile && emacs someFile

what would happen is that I would create a file named someFile and then I would immediately open it with Emacs (if you don’t have Emacs, you can use less). However, try this:

mislav@mislavovo-racunalo:~/Linux_folder$ ls -l nonExistentFile && emacs someFile

ls: cannot access 'nonExistentFile': No such file or directory

What happened here is that we tried to use ls on a non existent file and we got an error message. But our Emacs didn’t open up as well. What is happening here?

See, when you use the && operator (also known as the “and” operator) you are saying “Execute the first command and then, if it succeeds, execute the second command”. How does the shell know if a command executed successfully? Why, by exit code, of course! (Ward, 2014)⁠

When a command is successful, its exit code is 0. && says “I will execute the second command only if the exit code of the first command (to the left of the && sign) is 0”. ||, another logical operator, says just the opposite: “I will execute the second command only if the exit code of the first command (to the left of the || sign) is not 0”. You can remember it like this, but this makes more sense if you know about logical gates, which you don’t need to to understand Linux, so I won’t explain those.

So if I write:

mislav@mislavovo-racunalo:~/Linux_folder$ ls -l nonExistentFile || emacs someFile

this actually opens up Emacs, because || works in the way I described above.

Hope you learned something useful!

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 258