- ITDM 2025 전망 | “비전을 품은 기술 투자, 모두가 주춤한 시기에 진가 발휘할 것” 컬리 박성철 본부장
- 최형광 칼럼 | 2025 CES @혁신기술 리터러시
- The Model Context Protocol: Simplifying Building AI apps with Anthropic Claude Desktop and Docker | Docker
- This robot vacuum and mop performs as well as some flagship models - but at half the price
- Finally, a ThinkPad model that checks all the boxes for me as a working professional
The logic of && and || on Linux
$ 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!