Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

77%
+5 −0
Q&A How to invert command exit code?

An exclamation mark followed by space in the beginning of a pipeline will negate the final exit code of the pipeline. Here's an example in Bash. Echoing $? will print out the exit code of the prev...

posted 1y ago by Iizuki‭  ·  edited 8mo ago by Iizuki‭

Answer
#2: Post edited by user avatar Iizuki‭ · 2024-03-19T11:40:28Z (8 months ago)
Grammar
  • An exclamation mark followed by a space in the beginning of a pipeline will negate _the final exit code_ of the pipeline.
  • Here's an example in Bash. Echoing `$?` will print out the exit code of the previous command (which is also an echo in this case).
  • ```bash
  • $ echo OpenStreetMap is the best map
  • OpenStreetMap is the best map
  • $ echo $?
  • 0
  • $ ! echo OpenStreetMap is meh
  • OpenStreetMap is meh
  • $ echo $?
  • 1
  • ```
  • This is a [Posix thing](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_02).
  • An exclamation mark followed by space in the beginning of a pipeline will negate _the final exit code_ of the pipeline.
  • Here's an example in Bash. Echoing `$?` will print out the exit code of the previous command (which is also an echo in this case).
  • ```bash
  • $ echo OpenStreetMap is the best map
  • OpenStreetMap is the best map
  • $ echo $?
  • 0
  • $ ! echo OpenStreetMap is meh
  • OpenStreetMap is meh
  • $ echo $?
  • 1
  • ```
  • This is a [Posix thing](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_02).
#1: Initial revision by user avatar Iizuki‭ · 2023-11-03T08:34:03Z (about 1 year ago)
An exclamation mark followed by a space in the beginning of a pipeline will negate _the final exit code_ of the pipeline.

Here's an example in Bash. Echoing `$?` will print out the exit code of the previous command (which is also an echo in this case).
```bash
$ echo OpenStreetMap is the best map
OpenStreetMap is the best map
$ echo $?
0
$ ! echo OpenStreetMap is meh
OpenStreetMap is meh
$ echo $?
1
```

This is a [Posix thing](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_02).