Showing posts with label BASH. Show all posts
Showing posts with label BASH. Show all posts

6 Bash Productivity Tips

1. Use !!
Typing !! followed by Enter will bring back and execute the last command (same as Ctrl+P followed by Enter).

2. Use Emacs-like shortcuts
Emacs-like shortcuts make use of the Ctrl and Alt keys, and the big advantage of navigating and manipulating text in a Bash prompt is that you won't have to move your fingers away from the typing position (to access the arrow keys, or the Home, Page Up and Page Down keys). Notice that most of those work well inside manual pages too, or when using the Nano editor, or the less and more pagers.

Ctrl+D - or EOF (end-of-file) will quit the current Bash session
Ctrl+P - bring back the last command executed
Ctrl+N - bring the next command executed
Ctrl+U - delete all from the cursor to the left
Ctrl+K - delete all from the cursor to the right
Ctrl+B - go back one character
Ctrl+F - go forward one character
Alt+B - go back one word
Alt+F - go forward one word

3. Make manual pages coloured
I think this tip is a productivity tip because once a manual page is shown coloured, it is easier, clearer to read. Just add the following inside the ~/.bashrc file, where ~ is your home directory (notice that each line starts with export):

export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
export LESS_TERMCAP_md=$'\E[01;38;5;74m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # end mode
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_so=$'\E[38;5;246m' # begin standout-mode - info box export LESS_TERMCAP_ue=$'\E[0m' # end underline
export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline

Then, type:

source ~/.bashrc

And try any manual page (e.g. man bash). Here's how a coloured man page looks like using the presets above:

Coloured man page

4. Use your own scripts
If you have basic knowledge of Bash scripting, you can create scripts and put them in a directory included in your $PATH. You can use something like ~/bin or ~/usr/bin, where ~ is your home directory.

5. Copy/paste using Shift+Insert
Although this one is not Bash-specific, it's very useful when you need to quickly copy/paste some text in your terminal. The same can be accomplished using the middle-click mouse button.

6. Use aliases
One of the powerful features of Bash is that it makes use of aliases. Those are usually short commands which perform some associated, longer command or commands. For example, to update and upgrade your Ubuntu installation you would type sudo apt-get update && sudo apt-get upgrade, and to create an alias for it you would only have to add something like the following inside the ~/.bashrc file:

alias update='sudo apt-get update && sudo apt-get upgrade'

Now, each time you type update, the command inside the single quotes will be executed.

Understanding Bash (Part 2)

I like to customise my BASH prompt to be a bit more colourful and informative. You can also do it. But before that you must understand some basis syntax. There are more to the options below


Selected Options for Bash Prompt:
\h (Hostname)
\u (Username of the current user)
\w (current working directory, with the top-level directory as ~)
\W (base name of the current working directory, with top-level directory as ~)


How do we get BASH to look like below under CentOS
[root@test ~/scratch/tmp]$
# vim .bashrc
# export PS1='\e[1;33m[\u@\h \w]\$ \e[m'

\e[a;bm \e[m
(marks the start of the character the colour apply to.)
(a;bm is the color code)
(a can have a value of both 0 and 1. 1 will be the lighter colour of the 0)


For Color Codes, see Changing Colour for ls on BASH Blog

Understanding Bash (Part 1)

When you logged on there are 4 files that are associated with BASH

  • .bash_history (list of previously entered commands)
  • .bash_logout (run a script when Bash exit)
  • .bashrc (basic settings for prompt actions)
  • .bash_profile (additional configuration. If .bash_profile is not present, the user account will use /etc/profile)

Adding a Directory to a Path
To add a directory to a path, edit the .bash_profile
$ vim .bash_profile

------------------------------
PATH=$PATH:/usr/local/bin
export PATH
------------------------------

Putting Aliases
One of the appropriate place to put aliases is in .bashrc

Changing Color for ls on Bash

Adapted from Paint your BASH (pdf) by Heike Jurik (Linux Magazine)

Have you noticed that on a tty console, the colour of the directory is dark blue. It is so hard to read the blue text. How do we change the colours?

Step 1: Pipe the information from Dircolors and save as .dircolors
$ dircolors -p > ~/.dircolors

Step 2: Change the contents of .dircolors
$ vim .dircolors

#------------------------------------------------
NORMAL 00 # global default, although everything should be something.
FILE 00 # normal file
DIR 01;34 # directory
.........
#------------------------------------------------

Color Codes (Foreground):
00;30=Black
00;31=Red
00;32=Green
00;33=Brown
00;34=Blue
00;35 Magenta
00;36=Cyan
00;37=White

01;30=Grey
01;31=Light Red
01;32=Light green
01;33=Yellow
01;34=Light Blue
01;35=Pink
01;36=Light Cyan
01;37=Light Gray


Introduce DirColors to the new configuration
$ dircolors -b ~/.dircolors

Horray done.


Articles:

Understanding the difference between .bash_profile, .bash_logout and .bashrc

Important Notes on bash file
  1. These files in your home directories have special meaning to bash, providing a way to setup the account environment when you log in and when you invoke another bash command
  2. If the files are not found in your home directory, you are using the default system on /etc/profile
  3. Most important is the .bash_profile which is read and the command in it is executed by bash every time you logged on to the system.
  4. .bashrc is invoked when you start a new shell or by typing bash on the command line.
  5. If you just need to have the same command run regardless of  a subshell or a login shell, you can easily source .bashrc and hence execute .bashrc from within .bash_profile
  6. .bash_logout is read and executed every time a login shell exits.

Bash debugging

To help you with debugging your BASH scripts, you can use set this option which will print command traces before executing command

# set -o xtrace

Try it and you will love it.

For further notes, do read the
  1. Debugging Bash scripts by Bash Guide for Beginners

Adding Date and Time to Bash History

Taken from HowToForge "Adding Date and Time to your Bash History"

# vim /etc/bashrc


Type:
export HISTTIMEFORMAT="%h/%d - %H:%M:%S "