Bash Tips
Collection of Bash tips and tricks
-
Find append found paths
TLDR
find <DIRECTORY> -type f -exec <COMMAND> {} '+' -
Prevent single commands from being logged to Bash History
TLDR
<COMMAND>; history -d $((HISTCMD-1)) -
Redirect local standard-in to remote over SSH
TLDR utilize here-doc or here-string
-
List content of path one per line
TLDR
ls -1ensures output is one directory or file per line -
Pass multiple lists to function by reference
TLDR
local -n list_name -
Reverse list example
TLDR use a
forloop over indices -
Function keyword definition
TLDR
man --pager='less -p "Shell Function Definitions"' bash -
Define custom functions instead of using `eval`
TLDR use
now(){ date +'%Y-%m-%d %H:%M:%S'; } -
List pattern substitution example
TLDR use
/<pattern>/<substitution>or//<pattern>/<substitution>to find/replace first or all occurrences in a list -
Heredoc to variable via `read`
TLDR
read -r -d '' _var <<EOF -
Log upgrades via `script` command
TLDR
script -ac "sudo apt-get upgrade" "$(date +'%Y-%m-%d')_sudo_apt-get_upgrade.script" -
Sanitize input string
TLDR
printf '%q\n' 'some th!ng n@$ty' -
Searching `man` via pager
TLDR
man --pager='less --pattern=" -P pager"' man