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 examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe