Bash - yszheda/wiki GitHub Wiki

Tip Docs

Tools

Debug Bash

  • set -n or set -o noexec: checks for syntax errors without actually running the script
  • set -v or set -o verbose: echoes each command before executing it.
  • set -x or set -o xtrace: echoes the result each command, but in an abbreviated manner.
  • set -u or set -o nounset: gives an unbound variable error message and aborts the script
  • set -e or set -o errexit: Abort script at first error, when a command exits with non-zero status (except in until or while loops, if-tests, list constructs)

key binding

delete last word

Alt + Backspace

color console

echo line break

add header/tailer

read input

shell var in sed

shell var in awk

grep

grep only show filename

search in a given file type

   -G --file-search-regex PATTERN
          Only search files whose names match PATTERN.

surrounding lines

grep -B 3 -A 2 foo README.txt
grep -C 3 foo README.txt

exclude dir

ag

array

associative array / hashtable

# Bash 4
animals=( ["moo"]="cow" ["woof"]="dog")
# animals['key']='value' to set value
# "${animals[@]}" to expand the values
# "${!animals[@]}" (notice the !) to expand the keys

has element

working dir

pushd / popd

ls

numerical sort

-v — natural sort of (version) numbers within text

default var

calculation

round

printf

comp

char

is number

to array

find

find only show filename

find empty files

find by date

find /path ! -type f -newermt "YYYY-MM-DD HH:MM:SS" -delete. 

skip “permission denied”

depth

http://stackoverflow.com/questions/4509624/how-to-limit-depth-for-recursive-file-list

OR / AND pattern

find . \( -name "*.xyz" -o -name "*.abc" \)

leaf subdirs

find . -type d -links 2

dirname

basename

xml

string

multi line

e.g.

export PATH=`echo $PATH | sed 's/:/\n/g' | grep -v ros | paste -sd ":" -`

regex

substr

$ long="USCAGol.blah.blah.blah"
$ short="${long:0:2}" ; echo "${short}"
US

PCRE

echo 'time="2020-07-24 14:38:16" level=info msg="Networks: map[live:10.142.128.12]"' \
| grep -Po "(?<=live:).*(?=])"

# equivalent to:
echo 'time="2020-07-24 14:38:16" level=info msg="Networks: map[live:10.142.128.12]"' \
| grep -o "live:.*\]" \
| cut -d':' -f 2 \
| cut -d']' -f 1
  • (?=pattern): positive lookahead
  • (?!pattern): negative lookahead
  • (?<=): positive lookbehind
  • (?!=): negative lookbehind

occurrences

remove line break

split

to number

function

local var

return

stderr

condition

bracket / parentheses

The parameter list is too long.

add header

compare files

redirect output to array

parallel

loop through alphabet

find latest dir

abs path of script file

history

Unlimited Bash History

# Eternal bash history.
# ---------------------
# Undocumented feature which sets the size to "unlimited".
# http://stackoverflow.com/questions/9457233/unlimited-bash-history
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT="[%F %T] "
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
export HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# http://superuser.com/questions/20900/bash-history-loss
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

n. Setting HISTSIZE to a value less than zero causes the history list to be unlimited (setting it 0 zero disables the history list).

o. Setting HISTFILESIZE to a value less than zero causes the history file size to be unlimited (setting it to 0 causes the history file to be truncated to zero size).

Multiple terminals

# Avoid duplicates
HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend

# After each command, append to the history file and reread it
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

Remove duplicate

LANG, LC_CTYPE, LC_ALL

Terminal language

export LC_ALL=C

$PATH

trap

diff

diff -rq dir1 dir2
diff <(ls old) <(ls new)
diff -y <(xxd foo1.bin) <(xxd foo2.bin)

xxd / hexdump

printf '01%.0s' {1..31616} | xxd -r -p - input0.bin

$IFS

split

Time measure

seq

Opened file & Process

lsof

fuser

xargs

--no-run-if-empty / -r

sudo

sudo env PATH=$PATH 

stdbuf

less

zsh-like configuration

auto complete

bind 'set show-all-if-ambiguous on'
bind 'TAB:menu-complete'

history

⚠️ **GitHub.com Fallback** ⚠️