Bash or Bourne Again shell is the first thing that fascinated me in GNU/Linux. It has most of the
feature of some of the popular shell such as ksh, csh etc. From novice user to geekish hacker
everybody prefers bash for its powerful features. Bash is the default shell in all the GNU/Linux
distro coming nowadays, and not only Linux also other *nix operating system uses bash as their
shell. When I started using it, I began discovering lot of tricks which fasten my work which now
I'm sharing with you all. This article is for those who are already familiar Bash and who want to
work in lightening speed.
What's your editor?
The Bash works in two modes "Vi" mode and "Emacs" mode. In Vi mode the short cut keys
which are used in Vi editor can be applied over the bash, same like in Emacs mode it emulates
Emacs shortcut keys. you can decide which mode to work with based on your choice. Emacs
mode is set by default.
Set Emacs Mode in Bash:
set -o emacs
Set Vi Mode in Bash:
set -o vi
Shortcuts that work in Emacs mode:
Ctrl + l - Clear the screen
Ctrl + a - Move cursor to beginning of line
Ctrl + e - Move cursor to end of line
Alt + b - Move cursor back one word
Alt + f - Move cursor forward one word
Alt + backspace - Cut the word before the cursor
Ctrl + w - Cut the last word
Ctrl + u - Cut everything before the cursor
Ctrl + k - Cut everything after the cursor
Ctrl + y - Paste the last thing to be cut
Ctrl + r - Search the history
Ctrl + _ - Undo
And lot more Emacs shortcuts.
Below are the list of shortcut keys which working vi mode, Don't forget to hit escape before
executing any shortcut.
h - Move cursor left
l - Move cursor right
A - Move cursor to end of line and put in insert mode
0 (zero) - Move cursor to beginning of line (doesn't put in insert mode)
i - Put into insert mode at current position
a - Put into insert mode after current position
dd - Delete line (saved for pasting)
D - Delete text after current cursor position (saved for pasting)
p - Paste text that was deleted
u - Undo
Everyone will be familiar with the Tab key feature; it is widely used in auto completion in most
of the terminals. In bash tab key does lot of auto completion based on the first letter typed, here
"2T" means Press TAB twice.
$ 2T - List all available commands.
$ (string)2T - List all available commands starting with (string)
$ /2T - List entire directory structure including Hidden one
$ 2T - List only Sub Dirs inside including Hidden one
$ *2T - List only Sub Dirs inside without Hidden one
$ ~2T - List all Present Users on system from "/etc/passwd"
$ $2T - List all System variables
$ @2T - List entries from "/etc/hosts"
$ =2T - List output like ls or dir
Complete your last argument:
Are you tired of typing this
$cp important.txt /home/neo/archive/collection/important/confidential/personal
and you still have to copy many files?
$cp onemore.txt (Press Alt+. Or Esc and then .)
Bash will fill your last argument from the last command typed
$cp onemore.txt /home/neo/archive/collection/important/confidential/personal
Recollecting from history:
Bash has feature to recollect commands from the history, but instead of searching from the
history you can use '!' and then type first character of the command. It is one another way of
executing from the history for further information read man page of 'history'.
will expand and execute
$!/ will give the output of ifconfig
Push and pop to navigate between directories:
We can push our directories into a directory stack, and pop when needed. Sometimes we need to
access many long path of directories, we can save it by pushing it to directory stack. pushd and
popd are the builtin
used to push and pop the directories, they use $DIRSTACK variable. pushd
is like cd, but it also pushes the current working directory onto a stack before changing
directories. popd pops the top of the stack and returns you to the previous directory.
$ pushd /usr/share/doc/bash
Bash stores the previous directory path in the variable $OLDPWD. Suppose you have moved
from home directory to another directory and you want to get back to you home directory then
follow the example.
$ cd /usr/share
$ cd $
Auto corrects the path:
When ever we change to a directory using cd command it is possible to make mistakes while
typing the path. We can avoid getting stuck there using shopt. Using the below option we can
autocorrect our mistake and continue our job without any delay.
$ shopt -s cdspell
Some typo mistake and their correction by bash
$ cd /vra
Bind your keys:
As a regular Linux user, one will have some set of command which they will run often. Make it a
shortcut key so that you save your typing time spending jus one second. Bind command lets you
to map a key or key combination for an existing command or set of commands
Single shot to connect to your remote server:
Say you regularly connect to remote server for working, make it a shortcut key using below
$bind '"\M-k"':"\"telnet matrix\C-m\""
$bind '"\M-k"':"\"ssh neo@matrix\C-m\""
Type Alt+k and see the magic
Make your Dash board:
Monitoring a machine consist of many aspect such as memory, disk usage, process, network,
peripherals. Typing the commands and monitoring these components one by one is simply time
consuming and also tedious. Imagine dashboard functionality in a single key press 'F12'. Use this
example and add or remove monitoring command based on your choice.
bind '"\e[24~"':"\"dmesg|tail -n 6;cat /proc/net/dev;ps;w;uptime;df -h;cat /proc/meminfo\C-m"""
Press F12 Key and get the dashboard info
Lot more things can be done with bind for further information read bind documentation
Confirm your logout:
Many of us accidentally press Ctrld
(logout) when trying some short cut keys and gets logged
out of that session. To avoid it set the below line , it will ignore the EOF i.e. Ctrld
second time when you give Ctrld
it will log out, instead of 1 you can also make your own choice
Substitute and execute:
This tip is very useful when we are working in a remote machine especially low bandwidth or
high traffic connection. if you do a single mistake it will takes too much time to correct it.
The above command will fail since "share" is misspelled as "she". To correct it, instead of going
by native method of using arrow keys use the below example. This will substitute the misspelled
with correct and execute it.
The syntax is $^
(NOTE: It changes only the first occurrence of the pattern from left to right)
These trick will really help us to fasten our work, also makes life easier. Still there are many
hidden tricks in bash, it needs some time for me to consolidate it and make another part of this
article. For any queries/suggestion feel free to contact me at mail2krish[at]gmail[dot]com