Bash
Table of Contents
- 1. Shell movements
- 2. Interactive Shells
- 3. Bash Startup Files
- 4. Shell Parameter Expansion (
/#,##,%%etc) - 5. Redirections (
>,>>,<,2>&1etc) - 6. List function names and bindings
- 7. Bind a function named
watertoM-s f - 8. Display all locations containing an executable
- 9. Print the commands as they are executed
- 10. Check for unset variables
- 11. Pipe status exit codes
- 12. Date using
printfbuiltin - 13. Array of files basename
- 14. Unset values in array
- 15. Example with
trapbuiltin - 16. Change prompt displayed (
PS3) in aselectmenu - 17. Debug functions with
set -xandPS4 - 18. Functions
- 19. One liners
- 20. References
1. Shell movements
Shell movements
===============
Cursor position represented by []
---------------------------------
ctrl-a <-----------------> ctrl-e | moving
alt-b <--------> alt-f |
ctrl-b <--> ctrl-f |
$ mv file[].txt dir/
ctrl-w <--------> alt-d | erasing
ctrl-u <-----------------> ctrl-k |
2. Interactive Shells
3. Bash Startup Files
3.1. Prevent autostart for programs when inside an Emacs shell
To avoid running programs that may not render well within an Emacs shell when loading ~/.bashrc etc
if [[ -z $INSIDE_EMACS ]] then # Things to do when not using a shell inside Emacs fi
4. Shell Parameter Expansion (/#, ##, %% etc)
4.1. Return the same value from 2 different strings
youtube_link_channel="https://yewtu.be/feed/channel/foo" # OR youtube_link_channel="https://www.youtube.com/feeds/videos.xml?channel_id=foo" # both youtube_link_channel return "foo" youtube_channel_id="${youtube_link_channel##*[=/]}"
5. Redirections (>, >>, <, 2>&1 etc)
6. List function names and bindings
bind -p
7. Bind a function named water to M-s f
bind -x '"\esf":water'
8. Display all locations containing an executable
type -a foo
9. Print the commands as they are executed
++ in the output mean forking/subshell
set -x
<commands>
bash -x <file>
10. Check for unset variables
bash -u <file>
11. Pipe status exit codes
false | true | false | false echo "${PIPESTATUS[*]}"
12. Date using printf builtin
local date printf -v date '%(%Y-%m-%d)T' echo -n "$date"
13. Array of files basename
fn() { local files=(/path/to/files/file*) files=("${files[@]##*/}") declare -p files }
14. Unset values in array
# Iterate over the green blocklists for green_blocklist in "${green_blocklists[@]}" do # Remove green blocklists from the blue blocklists array for index in "${!blue_blocklists[@]}" do if [[ ${blue_blocklists[index]} == "$green_blocklist" ]] then unset 'blue_blocklists[index]' blue_blocklists=("${blue_blocklists[@]}") fi done done
15. Example with trap builtin
cleanup() { echo "bar" } main() { trap cleanup EXIT echo "foo" some-command } main
17. Debug functions with set -x and PS4
Single quotes for PS4 prevent early expansion of its value.
foo() { echo "hello" } bar() { echo "world" } main() { local PS4='+<${BASH_SOURCE[0]}><${BASH_LINENO[0]}><${FUNCNAME[0]}> ' set -x foo bar } main
+<./foo><19><main> foo +<./foo><15><foo> echo hello hello +<./foo><19><main> bar +<./foo><16><bar> echo world world
18. Functions
18.1. Wrap command (alternate buffer)
Send journalctl output to an alternate buffer, see notes:ansi for
the escape sequences.
journalctl() { printf '\e[?1049h' printf '\e[?25l' printf '\e[H' command journalctl "${@}" printf '\e[?1049l' printf '\e[?25h' }
18.2. Print 256 colors
lscolors() { local i tput bold for ((i=0; i<256; i++)) do # tput setaf "${i}" # printf 'color %s\n' "${i}" printf '\x1b[38;5;%dmcolor %d\n' "${i}" "${i}" done | column tput sgr0 }
18.3. Draw a colorful line
draw_line() { local color symbol nc n color="${1:-34}" # default to `blue' symbol="${2:--}" # default to `-' nc="$(tput cols)" for ((n=0; n<nc; n++)) do printf '\x1b[1;%dm%s\x1b[0m' "${color}" "${symbol}" done }
19. One liners
19.1. Find dotfiles packages not managed by Ansible
while read -r pkg; do if grep -rq "$pkg" ~/projects/ansible-dotfiles; then echo "pkg found: $pkg"; else echo "pkg not found: $pkg"; fi done < ~/.dotfiles/.pkg*
19.2. Flatpak remove all document ids for exported files
while read -r id; do flatpak document-unexport --doc-id "$id" && echo "done $id"; done < <(flatpak documents)