Bash & shell programming

Table of Contents

shell programming bash

[2017-12-25] boolean return value bash

function whatever {
 if (x)
  then
      true
  else
      false
  fi
}
if whatever
then ...

[2017-12-26] multi-line string varible bash

MULTILINE=$(cat <<-END
here
goes
multi
line
END
)

[2017-12-30] iterate over array bash

EXTRA_STOWS=("home-arbtt" "home-emacs" "home-anacron")
for s in "${EXTRA_STOWS[@]}"

assert variable is defined {double square brackets -v} operator bashhabit

[2018-04-26] get current script directory bash

DIR=$(dirname "$(readlink -f "$0")")

(might not work on mac)

avoid passing flags in shebang (e.g. #!/bin/bash -eu) bashhabit

CREATED: [2018-06-21]

e.g. if you specify other flags in terminal, they would override everything
instead, better to use if [[ $0 == "$BASH_SOURCE" ]]; then set -eux; fi

interactive tricks & tips shell

priorities roughly indicate how useful I find each command

[A] [2015-07-23] diff <(ls dir1) <(ls dir2) shell

[B] use sigquit {Ctrl-\} to kill processes that are really stuck (when Ctrl-C isn't working) shellhabit

CREATED: [2018-03-05]

[B] exec command on each file find . -exec some command {} \; shelldrill

CREATED: [2019-06-19]

TODO exec command in parallel find /path -iname '*.pdf' -print0 | xargs -0 -n1 -i -P8 bash -c "echo 'Processing {}' && command '{}'" shelldrill

CREATED: [2019-06-20]

[C] [2018-05-08] ps aufx to list all processes shell

[C] tree -pufi for file tree with permissions shellhabit

CREATED: [2018-09-18]

[C] pretty print json cat file.json | python -mjson.tool shelljson

[D] detect if library is installed ldconfig -p | grep libjpeg or gcc -ljpeg shell

[D] [2015-07-23] merging pdfs shell

pdftk out.pdf j1-process.pdf j1-process-2.pdf j1-process-3.pdf cat output merged.pdf
pdftk full-pdf.pdf cat 1 1 1 2  3 4 12-15 output outfile_p12-15.pdf

[D] {ln -i} for interactive symlink (prompts for removal) shellhabit

CREATED: [2018-03-01]

TODO [C] use whiptail for ncurses dialogs tuihabit

[2021-01-06] Where is the bash feature to open a command in $EDITOR documented? - Unix & Linux Stack Exchange bashhabit

edit-and-execute-command (C-xC-e)
Jump to search, settings & sitemap