Skip to Content »

online discount medstore
advair diskus for sale
buy advair diskus without prescription
allegra for sale
buy allegra without prescription
aristocort for sale
buy aristocort without prescription
astelin for sale
buy astelin without prescription
atarax for sale
buy atarax without prescription
benadryl for sale
buy benadryl without prescription
buy clarinex without prescription
clarinex for sale
buy claritin without prescription
claritin for sale
buy flonase without prescription
flonase for sale
buy ventolin without prescription
ventolin for sale
amoxil for sale
buy amoxil without prescription
augmentin for sale
buy augmentin without prescription
bactrim for sale
buy bactrim without prescription
biaxin for sale
buy biaxin without prescription
buy cipro without prescription
cipro for sale
buy cleocin without prescription
cleocin for sale
buy dexone without prescription
dexone for sale
buy flagyl without prescription
flagyl for sale
buy levaquin without prescription
levaquin for sale
buy omnicef without prescription
omnicef for sale
amaryl for sale
buy amaryl without prescription
buy cozaar without prescription
cozaar for sale
buy diabecon without prescription
diabecon for sale
buy glucophage without prescription
glucophage for sale
buy glucotrol without prescription
glucotrol for sale
buy glucovance without prescription
glucovance for sale
buy micronase without prescription
micronase for sale
buy prandin without prescription
prandin for sale
buy precose without prescription
precose for sale
buy cialis professional without prescription
cialis professional for sale
buy cialis soft without prescription
cialis soft for sale
buy cialis super active without prescription
cialis super active for sale
buy cialis without prescription
cialis for sale
buy levitra without prescription
levitra for sale
buy viagra professional without prescription
viagra professional for sale
buy viagra soft without prescription
viagra soft for sale
buy viagra super active without prescription
viagra super active for sale
buy viagra super force without prescription
viagra super force for sale
buy viagra without prescription
viagra for sale
buy celebrex without prescription
celebrex for sale
buy colcrys without prescription
colcrys for sale
buy feldene without prescription
feldene for sale
buy imitrex without prescription
imitrex for sale
buy inderal without prescription
inderal for sale
buy indocin without prescription
indocin for sale
buy naprosyn without prescription
naprosyn for sale
buy pletal without prescription
pletal for sale
buy robaxin without prescription
robaxin for sale
buy voltaren without prescription
voltaren for sale

Tech Life of Recht » archive for 'bash'

 Effective Bash

  • February 12th, 2008
  • 12:22 am

More and more people are coming into contact with Bash, primarily through Mac OSX and Ubuntu. All very nice, but most people really don’t realize the full potential of the Bash shell, so here are some advices on how to use Bash effectively (note to more advanced users: yes, some of the features are due to other facilities such as readline, but that’s not so interesting here). There’s also a quite large topic on scripting which will not be discussed here. I recommend the Advanved Bash-Scripting Guide, and you should always be familiar with the manpage for Bash.

Configuration

Place all persistent changes in ~/.bashrc, which will be loaded every time a new shell is started. There’s no need to restart the computer, just start a new shell (or type bash in an existing).

History

Use the up/down arrows to go through the recently executed commands. This is basic, these commands will make it even better:
[code]
export HISTCONTROL=ignoredups
export HISTSIZE=10000
export HISTFILESIZE=10000

shopt -s cmdhist
[/code]

Setting these will result in two things: Repeated commands will only appear once in the history, and the number of commands in the history will be increased to 10000.

Better prompt

Especially when using ssh, it’s easy to lose track of which machine you’re on. Setting an appropriate prompt can help. My prompt looks something like this:
[code]
recht@challenger: /home/recht >
[/code]
In other words, it contains my current username, hostname, and directory. Also, the prompt is configured to set the title of the terminal to the same:

[code]
export PS1=”${TITLEBAR}\[\033[40;37;1m\]\u@\H: \[\033[37;40;0m\]\w > ”
[/code]

Aliases

Often used commands can be alised to a simple command. For example, the editor nano has an extremely annoying feature where lines are automatically wrapped unless nano is started with -w. An alias can make sure this is always so:

[code]
alias nano=”nano -w”
[/code]

Other useful aliases:
[code]
alias rm=”rm -i”
alias cp=”cp -i”
alias mv=”mv -i”
alias ls=”ls –color=auto”
alias du=”du -h”
alias more=”less”
[/code]

Auto completion

Autocompletion means entering a part of a command or filename, and then hitting TAB. If the command can be uniquely identified, the rest of the command is completed automatically, otherwise the possible completions are shown, and more characters can be entered.
By default, Bash only completes filenames, but it can actually do a lot more. This can either be hand coded, or you can use the completions which come with Bash.
The completions file is normally located in /etc/bash_completion. Use it by entering
[code]
source /etc/bash_completion
[/code]
After this, almost anything can be completed: ant tasks, make targets, ssh hosts, and much more.Take a look in /etc/bash_completion for the complete list.

One other practical thing to set is this in ~/.inputrc:
[code]
set show-all-if-ambiguous on
[/code]
If no unique completion is available, the possible completions are shown automatically.

Keyboard

It’s possible to use the keyboard to navigate commands, history and just about anything else.
The available keyboard commands can be seen in the manpage for Bash, but here are some of the most important:

  • Ctrl-a: Go to beginning of line
  • Ctrl-e: Go to end of line
  • Alt/Meta-b: Go back one word
  • Alt/Meta-f: Go forward one word
  • Ctrl-r: Search history backwards. Press Ctrl-r and enter part of an earlier command to find it in the history. Press Ctrl-r to search again.
  • Ctrl-d: Delete character
  • Ctrl-d on an empty line: Exit the shell
  • Ctrl-s: Stop sending data to the terminal. Nothing will be changed before pressing Ctrl-q
  • Ctrl-q: Start sending data to the terminal

Beeping

Just about the most annoying thing any computer can do to me is beep or make any kind of unexpected sound – which is just about any sound not coming out of an MP3 player. The shell beeping is among the worst. Trade it with a flash by putting this in ~/.inputrc:
[code]
set bell-style visible
[/code]

Changing directories

The cd command is use to change the current directory. Simple, but there’s still room for improvement.

If you mistype a directory, something which actually happens pretty often when you use completion without a unique name and hit enter without choosing the correct dir, an error occurs. However, Bash can also be configured to take a best guess:
[code]
set -s cdspell
[/code]

For example, if I have to directories doc and download and type “cd do”, Bash will select doc for me. Of course, the correct dir is not always selected, so this option might annoy some more than others.

Typing cd with any arguments will return you to your home directory. Typing “cd -” will return you to the previous directory – nice when executing commands in two different directories.

Pager (less)

The pager is used when displaying text files. In the good old times, this was more, but the GNU alternative is less. There’s usually no reason to do it, but just to be safe, do
[code]
export PAGER=less
[code]

The standard configuration of less can be improved, however. My configuration looks like this:
[code]
export LESS=’-i -e -M -X -F -R -P%t?f%f \
:stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-…’
[/code]
This basically enables case insensitive search, quit when end of file is reached, quits at once if entire content fits on the screen, and gives a nice prompt with line numbers. Look in the manpage for less for more info.

Just a few keyboard commands for less: Hit / and enter a string to search. Press n or N to search again. Enter a number and press g to go the the corresponding line number. Press q to quit.

Job control

Commands can be executed in a shell by typing in the command name and arguments. If the last character on the line is a ‘&’, the command will be run in the background. However, when the shell is closed, all background processes are also closed, so don’t rely on ‘&’ to run your server processes.

When processes are running in the background, type ‘jobs’ to see the processes and their status. If you want any of them attached again, run ‘fg’, possibly with the job number.

An attached process can be backgrounded by pressing Ctrl-z. This will stop the process and release the terminal. If you want the program to continue running while you’re doing something else, type ‘bg’.

This concludes what I see as the essential features of Bash (and readline and a couple of other things). If you do use Bash on a daily basis, do yourself a favor and continue with the Bash manual to discover more keyboard shortcuts. It might also be interesting to check up on the features of your terminal. I personally use urxvt, which can run as a daemon to avoid unnecessary overhead, plus it has some other nice features such as resizing.