linux:bash

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux:bash [2022/02/28 14:54] mhlinux:bash [2026/01/11 11:57] (current) – [Control Operators] mh
Line 113: Line 113:
 ---- ----
  
 +=== 2>/dev/null || true ===
 +
 +<code bash>
 +$ something 2>/dev/null || true
 +</code>
 +
 +This means:
 +
 +Try something, but if it errors (exit code ≠ 0): 
 +  * silence the error message (send to ''/dev/null'')
 +  * immediately run ''true'' ( || operator passes along to second part)
 +  * Overall ''exit'' status becomes success (exit code = 0)
 +
 +In plain English:
 +
 +“Try to do the cleanup.
 +If there’s nothing to clean, that’s fine. Move on.”
 +
 +This pattern is **not appropriate** when:
 +  * Failure is meaningful
 +  * You want to be alerted
 +  * Data loss is possible
 +  * You are debugging
 +
 +In those cases, we want ''stderr'' and non-zero exits.
 +
 +So this is not about hiding problems.
 +It’s about acknowledging expected non-problems.
 +
 +----
 +
 +=== && AND Operator ===
 +
 +This control operator tells bash to run a second command **only if the first command before it succeeded**.
 +
 +If the first command fails, the second command is entirely skipped.
 +
 +**Example**
 +<code bash>
 +$ rm hello.txt && echo "Deleted file."
 +rm: cannot remove 'hello.txt': No such file or directory
 +</code>
 +
 +Because the file did not exist, the ''echo'' command was not run.
 ===== Arguments ===== ===== Arguments =====
  
  • linux/bash.1646056467.txt.gz
  • Last modified: 2025/06/02 21:18
  • (external edit)