Check these links:
- https://github.com/progrium/bashstyle
- https://www.shellcheck.net/
- https://kvz.io/blog/2013/11/21/bash-best-practices/
- http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming/
- http://wiki.bash-hackers.org/scripting/obsolete
- https://google.github.io/styleguide/shell.xml
- https://github.com/SimonBaeumer/bash-tricks
#!/usr/bin/env bashset -o errexit
set -o nounset
set -o pipefail
readonly DIR="$(cd"$( dirname "$0")"&&pwd)"readonly RANDOM_ID=$(head -c 24 /dev/urandom | base64)# 33 chars
[[ "$TRACE" ]] &&set -o xtrace
main() {
if [[ $#-gt 0 ]];then"$@"else
default_cmd
fiecho"DONE!"
}
[[ "$0"=="$BASH_SOURCE" ]] &&time main "$@"clean_value() {
local v=${1,,}# lowercase
v="${v//[!a-z0-9-]/-}"# only a-z0-9-echo"${v:0:63}"# 63 max chars
}
URI_NO_SLASH_AT_END=${URI%%/}
cat <(echo "123")
base64 <(echo "123")# echo "123" | base64
cat <(base64 --decode <<EOFMTIzCg==EOF)is_osx() {
[[ $('uname')=='Darwin' ]]
}
is_linux() {
[[ $('uname')=='Linux' ]]
}
adddate() {
# adds data to output: e.g. somecommand | adddatewhile IFS= read -r line;doecho"$(date +"[%F %T]")$line"done
}
if!hash something 2>/dev/null;thenecho"'something' was not found"fi# list symlinks
find "$HOME" -maxdepth 2 -type l -exec ls -lah --color {} + 2>/dev/null | sed -e 's/.* \(.* -> .*\)/\1/'# add line if not exists
grep -Fq 'source ~/.myterminalrc'~/.bashrc ||echo'source ~/.myterminalrc'>>~/.bashrc
# skip N lines
tail -n +2 /tmp/csv_file_with_header.csv
# heredocs# https://stackoverflow.com/questions/1167746/how-to-assign-a-heredoc-value-to-a-variable-in-bash# https://stackoverflow.com/questions/4937792/using-variables-inside-a-bash-heredoc
VAR=$(cat <<'EOF'something'bla"$(dont-execute-this)foo"bar"'EOF)
VAR=$(cat <<EOFsomething'bla"$(do-execute-this)foo"bar"''EOF)# for each argument run command, in parallelprintf'%s\n'"$@"| xargs -I{} -P4 -t echo foo {} bar
# UTF-8 emojis at https://apps.timwhitlock.info/emoji/tables/unicode or https://unicode.org/emoji/charts/full-emoji-list.html
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'# No Colorcleanup_exit() {
rv=$?# do some handlingif [[ $rv!= 0 ]];then# echo with colors and emojis to stderr>&2echo -e "\\U1F6A8\\U1F6A8\\U1F6A8\\U1F6A8\\U1F6A8 \\U274C${RED} Failed! \\U1F446 ${NC}\\U1F6A8\\U1F6A8\\U1F6A8\\U1F6A8\\U1F6A8"exit$rvelse>&2echo -e "\\U2705${GREEN} Completed successfully!${NC}"fi
}
trap"cleanup_exit" SIGQUIT SIGTERM EXIT
# create base dir if not exists
mkdir -p "$(dirname "$someFilePath")"# check if variable is defined and non empty
[[ -n"${POSIBLY_EMPTY_VARIABLE:-}" ]]