Shell Tricks That Actually Make Life Easier (And Save Your Sanity)

Watch someone backspace 40 characters instead of pressing CTRL+W, and you’ll understand why this list exists. A collection of shell tricks-grouped by what works everywhere and what’s Bash/Zsh-speci...

Larvitz Blog

Sometimes zip files are rude and don't have a top directory. Here's a one-liner to unzip a bunch of archives into directories named after each file:

```
for f in *.zip; do unzip -d ${f/.zip/} $f; done
```

#bashTricks #shTricks #shellTricks

Bash has a way to specify strings with escaped characters like so: $'\r\n' which gives a string containing carriage return and newline characters.

The `read` built-in can take a delimiter with the -d flag. Combining these lets you use a null character: read -d $'\0'

Useful in some of the same places you might use xargs(1).

find . -name ... -print0 | while read -d $'\0'; do ./foo "$REPLY"; done

#bashTricks #xargs #bash #shellTricks

A common parsing task is finding lines that match a pattern but only if another pattern is seen first.

The MacOS utility otool(1) prints the load commands in a Mach-O binary with a header line followed by the info about that load command on several subsequent lines.

This one liner will show just the path for every imported dylib:

otool -l foo
| awk '/LC_LOAD_DYLIB/{ in_dylib_lc=1 }; in_dylib_lc == 1 && /name/ { in_dylib_lc = 0; print }'

#bashTricks #awk #shellTricks

redteamers take note, if your c2 can't handle moving whole directories very well.

usually works like a champ in places where they don't look at the network stack much or at all, or employs folks who openly say networking doesnt matter

#BashTricks #ShellTricks #ssh #postgresql

Want to read in a password from stdin during a demo without exposing it? This is what I used in my latest talk: `stty -echo && read PASSWORD && stty echo`. #ShellTricks

Capture STDOUT, STDERR and return status in shell variables

This second answer ( the one with the banana function ) shows how to capture all three to variables in bash without needing tmp files

https://stackoverflow.com/questions/13806626/capture-both-stdout-and-stderr-in-bash

----
banana() {
echo "banana to stdout"
echo >&2 "banana to stderr"
return 42
}
----

----
. <({ berr=$({ bout=$(banana); bret=$?; } 2>&1; declare -p bout bret >&2); declare -p berr; } 2>&1)
----

Then check values of $bout, $berr, $bret

#ShellTricks #bash #FLOX

Capture both stdout and stderr in Bash

I know this syntax var=`myscript.sh` or var=$(myscript.sh) Will capture the result (stdout) of myscript.sh into var. I could redirect stderr into stdout if I wanted to capture both. How to sav...

#unix #shelltricks piping to xargs when target command only support a single argument: echo "ssh cron" | xargs --max-args=1 systemctl status