“
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.
cd -
2. Execute Your Last Command
!!
3. Check the Return Code of Your Last Command
echo $?
4. Get the Process ID
SHELL
is not always defined by the bash shell:# echo $SHELL
ps -p $(echo $$)
PID TTY TIME CMD
26719 pts/0 00:00:01 bash
5. Execute the Emulating Tree Command
alias tree='function tree(){ find ${1:-.} | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/"; unset -f tree;}; tree'
6. Ad-hoc YAML Linter
alias ymllint='python -c "import sys,yaml as y;y.safe_load(open(sys.argv[1]))"'
7. Ad-hoc JSON Linter
alias jsonl='jq "." >/dev/null <'
8. Set the Sell
vi
or emacs
mode - no debate is needed:# echo $SHELLOPTS to check
set -o vi
set - o emacs
9. Globbing
a
:# * matches any sequence of characters
ls a*
10. Create a Nested Structure
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>:p
to print the command without running it:
"
Shell
alone is eternal, perpetual, immortal" - maybe Schopenhauer