These AND and OR equivalents can be used in scripts to determine next actions. Credit: NDAB Creativity / Shutterstock We’ve all probably seen the && and || operators in use from time to time. These AND and OR equivalents are often used in scripts to make decisions about what happens next. In this post, we’ll take a look at how they work at a very basic level and some ways they are often used in scripts. Using || To begin, the || (OR) operator aligns itself with the use of the word “or” in common day English. However, it makes a clear distinction. If the command on the left side of the || is successfully run, the command on the right is not run. Run the command below and you’ll never see it respond “goodbye”. $ echo hello || echo goodbye hello On the other hand, if the command on the left of the || fails, the one on the right side will be run. $ cat nosuchfile || echo oops! cat: nosuchfile: No such file or directory oops! You could get the same response (without the error) with an if command like this one: $ if [ ! -f nosuchfile ]; then > echo oops! > fi oops! If the commands on both sides of the || operator fail, you just see more than one error message. $ cat nosuchfile || cal nosuchmonth cat: nosuchfile: No such file or directory cal: failed to parse timestamp or unknown month name: nosuchmonth Using && The && operator uses different logic. It will run the commands on both sides of the && as long as the one on the left completes successfully. This is the AND logic – like “this AND that”. $ echo hello && echo goodbye hello goodbye If, on the other hand, the command on the left side fails, the command on the right side is not run. The AND logic (run both commands) simply gives up. The command line just stops with the initial failure. Notice that the “no surprise there” string below is not displayed. $ cat nosuchfile && echo no surprise there cat: nosuchfile: No such file or directory Combining || and && There’s no reason that you can’t use both || and && operators in a single command. Doing so just makes the parsing a bit trickier. Run a command like this and watch what happens: $ echo this || echo that && echo the other this the other The first echo command runs successfully, so the second isn’t run (normal OR behavior). The left side of the AND (&&) was still run successfully, so the right side runs as well. In the next example, we test whether the /etc/shadow file (the file which holds password encryptions etc.) can be opened for reading. Note the result. The file isn’t opened because the command is not being run with superuser privilege. Note also that error output is being sent to /dev/null so as not to be displayed. $ cat /etc/shadow 2>/dev/null && echo "file opened" || echo "file failed to open" file failed to open The failure of opening the /etc/shadow file means the command following the && (AND) is not run. Yet the command following the || (OR) operator is run because, well, that’s the way || works. Run the command with sudo and you’ll get a different response. Note, though, that I am sending the cat command’s output to /dev/null to avoid stuffing all those lines of output into this post! $ sudo cat /etc/shadow >/dev/null && echo "file successfully opened" || echo "file failed to open" file successfully opened Wrap-up The || (OR) and && (AND) operators can be very useful, but it’s always a good idea to test your logic to make sure it follows the rules you are expecting before entrusting them to a script. Related content how-to How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe