## Return Non-zero Exit Statuses from Pipe
```shell
(set -o pipefail && command | command)
```
> [!info] pipefail
> If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default.
## Use Subshells for Variables
https://unix.stackexchange.com/questions/56444/how-do-i-set-an-environment-variable-on-the-command-line-and-have-it-appear-in-c
```shell
(variable=value; echo $variable)
```
## Expand Alternatives
Use brace expansion `{a,b}` to expand into different alternatives. However, this won’t work unless all the globs match something.
```sh
open *.{jpg,png,gif}
```
Globs also support alternatives `(a|b)`, which does work if something doesn’t match.
```sh
open *.(jpg|png|gif)
```
## Reverse Lines
`cat` backwards!
```sh
tac
```
## Count Lines
```sh
wc -l
```
## Replace Part of a Command with Each Line
From [tldr](https://tldr.inbrowser.app/pages/common/xargs)
```sh
# Execute the command once for each input line, replacing any occurrences of the placeholder (here marked as `_`) with the input line:
xargs -I _ command _ arg2 arg3
```
## Open Stdin in Code
For [Visual Studio Code - Code Editing. Redefined](https://code.visualstudio.com/)
```sh
code -
```
## Find and Replace, Showing only the Replacement
For ripgrep
```sh
rg pattern file -or replacement
```
- `-o` Shows only the match
- `-r` Performs replacement
## Install an Older Version of a Homebrew Formula
```sh
brew tap-new $USER/local-tap
# extract with a version seems to run a `git log --grep` under the hood
brew extract --version=4.4.23 bash $USER/local-tap
# Install your new version from the tap
brew install
[email protected]
# Note this "fails" trying to grab a bottle for the package and seems to have
# some odd doubling of the version in that output, but this isn't fatal.
```
https://stackoverflow.com/questions/3987683/homebrew-install-specific-version-of-formula/55764594#55764594