Yet Another Guide About Bash tricksby@dejanualex
418 reads
418 reads

Yet Another Guide About Bash tricks

by dejanualexSeptember 10th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The shell is an inherent part of everyday life of a software engineer. I would dare to say that any “trick” that can improve the general speed or knowledge regarding this matter is quite important... From one-liners to aliases, the following compilation will help you improve your speed in a noticeable way.

Company Mentioned

Mention Thumbnail
featured image - Yet Another Guide About Bash tricks
dejanualex HackerNoon profile picture

Shell leads the willing and drags along the reluctant “ -maybe Seneca

Nowadays, working with a shell is an inherent part of the everyday life of a software engineer, therefore, I would dare to say that any “trick” that can improve the general speed or knowledge regarding this matter is rather important.

Without further ado, I will share a compilation of different commands which has helped me greatly.

10 Bash Commands to Improve Speed

  1. Go Back to the Previous Directory

  • Going back to the previous directory (based on $OLDPWD internal variable) - especially useful when navigating through long paths:
cd -

2. Execute Your Last Command

  • Execute last command - useful for rerunning the previous command:

3. Check the Return Code of Your Last Command

  • Check the return code of the last command - useful when checking various return codes (in Unix and Linux, every command returns a numeric code between 0-255):
echo $? 

4. Get the Process ID

  • Get the Process ID of this shell itself - SHELL is not always defined by the bash shell:
# echo $SHELL
ps -p $(echo $$)
26719 pts/0    00:00:01 bash

5. Execute the Emulating Tree Command

  • Emulating tree command - on minimal distros, tree command may not exists, therefore the following alias provides the much needed recursive directory listing:
alias tree='function tree(){ find ${1:-.} | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/"; unset -f tree;}; tree'

6. Ad-hoc YAML Linter

  • Ad-hoc YAML linter as alias - not a proper linter but useful for quick check of various YAML files:
alias ymllint='python -c "import sys,yaml as y;y.safe_load(open(sys.argv[1]))"'

7. Ad-hoc JSON Linter

  • Ad-hoc JSON linter as alias - based on jq JSON processor:
alias jsonl='jq "." >/dev/null <'

8. Set the Sell

  • Set the sell to work in either vi or emacs mode - no debate is needed:
# echo $SHELLOPTS to check
set -o vi
set - o emacs

9. Globbing

  • globbing “searching for files using wildcard expansion”-list files that start with the lettera:
# * matches any sequence of characters
ls a*

10. Create a Nested Structure

  • Creating a nested structure of directories:
mkdir -p provisioning/{datasources,notifiers,dashboards/backup}

# will create
└── provisioning
    ├── dashboards
    │   └── backup
    ├── datasources
    └── notifiers

11. Reverse search

  • Want to re-run a lengthier and complicated command and reverse-search (CTRL+R) is not enough, just use history to list the previously used commands and then !<line_no>

    to re-run command or !<line_no>:pto print the command without running it:


" Shell alone is eternal, perpetual, immortal" - maybe Schopenhauer