The &&, ||, and ! operators allow you to run a lot of useful commands on Linux, but you first need to get used to syntax that is a little trickier than the if-then-else command you might normally use.\nTo get started, I should explain that one thing the command examples in this post have in common is the use of something that I might call a shorthand \u201cif\u201d test. Here\u2019s an example:\n$ [ -f donuts ]\n$ echo %?1\nThe -f donuts command asks if there is a file (-f) named \u201cdonuts\u201d. Unless we display the return code afterwards with "echo $?", the result of the test is not displayed. In this case, it\u2019s false (i.e., not zero), so we know the file doesn\u2019t exist. No donuts for us!\nTo compose a similar but opposite test, we could ask if no donuts file exists. That test would look like this:\n$ [ ! -f donuts ]\n$ echo $?\n0\n\nThat time, the result is true (0). No donuts again. However, this isn\u2019t how these commands are generally used. Instead, && or || operators are added to the end to run some command based on the results of the test that is run. For example, if there is no donuts file, you might want to lodge a complaint like this:\n$ [ ! -f donuts ] && echo \u201cWe need donuts!\u201d\n\nThe key is to understanding this command is that\u00a0&&\u00a0is an "AND" operator while\u00a0||\u00a0is an "OR" operator. If you run a command like the one shown above, both sides of the command -- the test and the resulting command -- will run if there is no file named "donuts". The command basically says that "if a file named\u00a0donuts\u00a0does not exist, display the text using the echo command. Using &&, whenever the result of the test using\u00a0is true, the command on the right side of the && is run. If the result is false, the command ends.\nThis command says \u201cif there is a directory named mydir\u201d, display a listing:\n$ [ -d mydir ] && ls -ld mydir\ndrwxr-xr-x. 2 shs shs 4096 Apr 11 11:46 mydir\n\nIn this next example, we test whether a directory does not exist. If it doesn\u2019t, the mkdir command will create one. The second command shown below verifies that the directory was created. If the directory exists already, the command will yield a failure status and the mkdir command will not be run.\n$ [ ! -d newdir ] && mkdir newdir\n$ ls -ld newdir\ndrwxr-xr-x. 2 shs shs 4096 Apr 11 11:54 newdir\n\n\n\n\n\n\nThe opposite is true when you use ||. If the first part of the command succeeds (yields \u201ctrue\u201d), the second part is never run.\nFor && operators, the following logic is implemented:\nIf RESULT then ACTION\n======================\nIf SUCCESS then RUN 2nd command\nIf FAIL then END\n\nIf you run a command using\u00a0||, the second part of the command will only run if the first fails. If the first part is successful, the command is finished. In other words, the overall command is successful if either part works.\nFor || operators, the following logic is implemented:\nIf RESULT then ACTION\n======================\nIf SUCCESS then END\nIf FAIL then RUN 2nd command\n\nIn this next example, we ask if there is a directory named \u201cscripts\u201d. If there isn\u2019t (i.e., the test result is \u201cfalse\u201d), the second part of the command is run and\u00a0the mkdir command creates it. If there is a scripts directory, the command ends and the mkdir command is not run.\n$ [ -d scripts ] || mkdir scripts\n$ ls -ld scripts\ndrwxr-xr-x. 2 shs shs 4096 Apr 11 12:16 scripts\n\nWrap-Up\nEven though && and || behave differently, you can use either of the operators to get the same result. The examples below illustrate this.\nEach of the commands below will create a file named notes if one doesn\u2019t exist and do nothing if it does.\n$ [ ! -f notes ] && touch notes\n$ [ -f notes ] || touch notes\n\nSimilarly, each of the two commands below creates a scripts directory if one doesn\u2019t already exist and otherwise make no changes.\n$ [ ! -d scripts ] && mkdir scripts\n$ [ -d scripts ] || mkdir scripts\n\nWhile using && and || may seem a bit tricky, a little practice and some handy example commands can really pay off.