Skip to main content
Add correct syntax hints, break into list
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77
echo "anything" | { grep e || true; }
echo "anything" | { grep e || true; }

Explanation:

$ echo "anything" | grep e
### error
$ echo $?
1

$ echo "anything" | { grep e || true; }
### no error
$ echo $?
0

### DopeGhoti's "no-op" version
### (Potentially avoids spawning a process, if `true` is not a builtin):
$ echo "anything" | { grep e || :; }
### no error
$ echo $?
0
  • This will throw an error
    $ echo "anything" | grep e
    $ echo $?
    1
    
  • This will not throw an error
    $ echo "anything" | { grep e || true; }
    $ echo $?
    0
    
  • DopeGhoti's "no-op" version (Potentially avoids spawning a process, if true is not a builtin), this will not throw an error
    $ echo "anything" | { grep e || :; }
    $ echo $?
    0
    

The || means "or". If the first part of the command "fails" (meaning grep e returns a non-zero exit code) then the part after the || is executed, succeeds and returns zero as the exit code (true always returns zero).

echo "anything" | { grep e || true; }

Explanation:

$ echo "anything" | grep e
### error
$ echo $?
1

$ echo "anything" | { grep e || true; }
### no error
$ echo $?
0

### DopeGhoti's "no-op" version
### (Potentially avoids spawning a process, if `true` is not a builtin):
$ echo "anything" | { grep e || :; }
### no error
$ echo $?
0

The || means "or". If the first part of the command "fails" (meaning grep e returns a non-zero exit code) then the part after the || is executed, succeeds and returns zero as the exit code (true always returns zero).

echo "anything" | { grep e || true; }

Explanation:

  • This will throw an error
    $ echo "anything" | grep e
    $ echo $?
    1
    
  • This will not throw an error
    $ echo "anything" | { grep e || true; }
    $ echo $?
    0
    
  • DopeGhoti's "no-op" version (Potentially avoids spawning a process, if true is not a builtin), this will not throw an error
    $ echo "anything" | { grep e || :; }
    $ echo $?
    0
    

The || means "or". If the first part of the command "fails" (meaning grep e returns a non-zero exit code) then the part after the || is executed, succeeds and returns zero as the exit code (true always returns zero).

echo "anything" | { grep e || true; }

Explanation:

$ echo "anything" | grep e
### error
$ echo $?
1 

$ echo "anything" | { grep e || true; }
### no error
$ echo $?
0 

### DopeGhoti's "no-op" version
### (Potentially avoids spawning a process, if `true` is not a builtin):
$ echo "anything" | { grep e || :; }
### no error
$ echo $?
0

The "||"|| means "or". If the first part of the command "fails" (meaning "grep e"grep e returns a non-zero exit code) then the part after the "||"|| is executed, succeeds and returns zero as the exit code (true always returns zero).

echo "anything" | { grep e || true; }

Explanation:

$ echo "anything" | grep e
### error
$ echo $?
1
$ echo "anything" | { grep e || true; }
### no error
$ echo $?
0
### DopeGhoti's "no-op" version
### (Potentially avoids spawning a process, if `true` is not a builtin):
$ echo "anything" | { grep e || :; }
### no error
$ echo $?
0

The "||" means "or". If the first part of the command "fails" (meaning "grep e" returns a non-zero exit code) then the part after the "||" is executed, succeeds and returns zero as the exit code (true always returns zero).

echo "anything" | { grep e || true; }

Explanation:

$ echo "anything" | grep e
### error
$ echo $?
1 

$ echo "anything" | { grep e || true; }
### no error
$ echo $?
0 

### DopeGhoti's "no-op" version
### (Potentially avoids spawning a process, if `true` is not a builtin):
$ echo "anything" | { grep e || :; }
### no error
$ echo $?
0

The || means "or". If the first part of the command "fails" (meaning grep e returns a non-zero exit code) then the part after the || is executed, succeeds and returns zero as the exit code (true always returns zero).

Avoid unsafe override of exitcode as per upvoted comment by JohnMudd
Source Link
echo "anything" | { grep e || truetrue; }

Explanation:

$ echo "anything" | grep e
### error
$ echo $?
1
$ echo "anything" | { grep e || truetrue; }
### no error
$ echo $?
0
### DopeGhoti's "no-op" version
### (Potentially avoids spawning a process, if `true` is not a builtin):
$ echo "anything" | { grep e || :; }
### no error
$ echo $?
0

The "||" means "or". If the first part of the command "fails" (meaning "grep e" returns a non-zero exit code) then the part after the "||" is executed, succeeds and returns zero as the exit code (true always returns zero).

echo "anything" | grep e || true

Explanation:

$ echo "anything" | grep e
### error
$ echo $?
1
$ echo "anything" | grep e || true
### no error
$ echo $?
0
### DopeGhoti's "no-op" version
### (Potentially avoids spawning a process, if `true` is not a builtin):
$ echo "anything" | grep e || :
### no error
$ echo $?
0

The "||" means "or". If the first part of the command "fails" (meaning "grep e" returns a non-zero exit code) then the part after the "||" is executed, succeeds and returns zero as the exit code (true always returns zero).

echo "anything" | { grep e || true; }

Explanation:

$ echo "anything" | grep e
### error
$ echo $?
1
$ echo "anything" | { grep e || true; }
### no error
$ echo $?
0
### DopeGhoti's "no-op" version
### (Potentially avoids spawning a process, if `true` is not a builtin):
$ echo "anything" | { grep e || :; }
### no error
$ echo $?
0

The "||" means "or". If the first part of the command "fails" (meaning "grep e" returns a non-zero exit code) then the part after the "||" is executed, succeeds and returns zero as the exit code (true always returns zero).

Comment re: builtin true
Source Link
John N
  • 2.1k
  • 1
  • 16
  • 17
Loading
Add DopeGhodi's no-op variant (1 less process)
Source Link
John N
  • 2.1k
  • 1
  • 16
  • 17
Loading
Source Link
John N
  • 2.1k
  • 1
  • 16
  • 17
Loading