Within the instance beneath, the head command fails as a result of it wasn’t run with root-level privilege (e.g., utilizing sudo) and, thus, has no entry to the /and so forth/shadow file. The echo command then runs after the error seems.
$ head -4 /and so forth/shadow || echo oops head: can't open '/and so forth/shadow' for studying: Permission denied oops
If you wish to see the impact with out the error message, you might ship the error output of the head command to /dev/null like this:
$ head -4 /and so forth/shadow 2>/dev/null || echo oops oops
The 2> operator redirects error output – on this case to /dev/null.
Utilizing > and >>
The > and >> operators, in contrast to | and ||, are tightly associated to one another. Nonetheless, they’ve a distinct although associated operate. For those who run a command like a kind of beneath, you add output of the fortune command to a file, creating it if is doesn’t exist already and overwriting it if it does.
$ fortune > readme $ cat readme 1 bulls, 3 cows. $ fortune > readme $ cat readme Does a one-legged duck swim in a circle?
Discover that, within the instance above, the content material of the file is changed.
For those who use >> as an alternative, content material will likely be appended to the file if it exists and used to create a brand new file if it doesn’t.