Created
October 4, 2018 13:22
-
-
Save zgracem/0f4f64dddf8ff324553c92049b419034 to your computer and use it in GitHub Desktop.
Miscellaneous syntax notes for bash 3–5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use command output as a string (command substitution) | |
echo "Today is $(date +%F)." | |
# Use the contents of a file as a string (command substitution) | |
# Good: | |
var=$(<stuff.txt) | |
# Bad: | |
var=$(cat stuff.txt) | |
# Ugly: | |
read var < stuff.txt | |
# Use a string as a file (here strings) | |
# Good: | |
egrep "$pattern" <<< "$string" | |
# Bad: | |
echo "$string" | egrep "$pattern" | |
# Worse: | |
echo "$string" > file; egrep "$pattern" file | |
# Use command output as a file (process substitution) | |
diff <(curl http://example.com) <(curl http://example.org) | |
# Accept both `moo "string"` and `moo < file` | |
moo() { local input=${@:-$(</dev/stdin)}; cowsay "$input"; } | |
# Display command output while also capturing it in a variable | |
stdin=$(tty) && today=$(date +%F | tee $stdin) | |
# Silently disable xtrace | |
{ set +x; } 2>/dev/null | |
# Reversible tilde expansion -- bash 3 & 4 compatible | |
echo "${PWD/#$HOME/$'~'}" | |
echo "${PWD/#$'~'/$HOME}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment