Shell script - kachick/dotfiles GitHub Wiki

Aside from 🤮, I need to write Shell script in several place.

This is a note for me.

test

file

  • -e: exists
  • -f: exists & regular # "regular" means both text and binary. In other words excluding directory, symlink and device
  • -d: exists & directory
  • -r: exists & the user has read permission
  • -w: exists & the user has write permission
  • -x: exists & executable
  • -N: exists & modified from last read
  • -s: exists & non empty

string

  • -n: non empty
  • -z: empty

set

  • -: Enable

  • +: Disable

  • -e: exit with non 0

  • -u: exit with undefined variable

  • -x: print executed command

  • -o: # TODO

  • pipefail: # TODO

assign value and print to tty

> dir="$(mktemp --directory | tee --append /dev/tty)"
/tmp/tmp.y2NaKkqILD
> echo $dir
/tmp/tmp.y2NaKkqILD

How to delegate multiline string to another command

If escape is a hard thing

  1. https://github.com/kachick/dotfiles/blob/beabf65a88671770b85029b71450fabee5c9868b/pkgs/fzf-bind-posix-shell-history-to-git-commit-message/fzf-bind-posix-shell-history-to-git-commit-message.bash#L9
  2. https://github.com/kachick/dotfiles/blob/beabf65a88671770b85029b71450fabee5c9868b/pkgs/safe_quote_backtik/safe_quote_backtik.bash#L1-L3