You are here

Bash Rules

Bash Rules
Checked: 1 hour 50 min ago
Updated: 1 hour 50 min ago
Update every: 2 hours

Subscribe to Bash Rules

Whats your background?

Bash Rules - Tue, 03/07/2007 - 7:20am
As part of job control its possible to stop a process and continue it later.
So while you are running a program, you can type Ctrl-Z to suspend it , its like a pause in a media player

Then you can ask bash to continue the process in background by typing 'bg'.
Background processes can not read from terminal and if they try to they will be suspended(paused).

Here is quick test for this:

bash$ cat

sleep 5
echo -n "Enter some text > "
read text
echo "You entered: $text"

Reviewing Sed - Part 1

Bash Rules - Wed, 27/12/2006 - 10:49pm
There is a good collection of sed one liners at

So I have just begun to review few them using sed manpage because of the earthquake in Taiwan.

# Following line will insert a blank space after everyline in temp.txt

sed G temp.txt

G - Append hold space to pattern space.

So hold space holds nothing initially and if you append that to pattern space, you get a new line.

So thats the way to add blank lines after everyline.

Sed and No DuPlication

Bash Rules - Wed, 13/12/2006 - 12:23am
I needed to remove duplicate names from a file so I thought sed should be a good choice for it.
uniq is too easy so I found out how sed does it. Herez how

$ sed '$!N; /^\(.*\)\n\1$/!P; D' filename

So now time for an explanation.

1 $!N - sed reads one line at a time and then works on it. It doesn't read in the newline at the end of the line into pattern space (what sed works on). So N command appends the newline and the next line to the pattern space.

Its time for tee

Bash Rules - Fri, 08/12/2006 - 12:19am
I was asked this question and suddenly i realized that bash doesnt have redirection like operator to do this. Do what? Print Standard Output/Error as it is and also redirect it to a file. So therefore its time for tee.

$ls i_dont_exist 2>&1 | tee output.txt
ls: i_dont_exist: No such file or directory
$cat output.txt
ls: i_dont_exist: No such file or directory

Colorful Bash

Bash Rules - Thu, 19/10/2006 - 3:08am
Well here is how(er..i mean how not to :)) to make your terminal a bit more colorful

[kalyan@duck snippets]$PROMPT_COMMAND='tput setf $(( RANDOM % 7 + 1 ))'

And so we get a colorful terminal

Bash Meditation

Bash Rules - Fri, 14/07/2006 - 12:52am

Here is the oneliner for meditation in bash

while :; do whoami; done

CSS Woes!!!

Bash Rules - Wed, 19/04/2006 - 2:45am
Well spent a good deal of time with Prateek debugging an issue with links not getting underlined
when <a name= is used and ended up with this

Migrate apps from Internet Explorer to Mozilla

Thank you guys :)

Bash : Order of expansions

Bash Rules - Sun, 16/04/2006 - 1:27am
This one is an important thing to understand for anyone who wants to know how bash works.
So here is a refresher, this is the order of expansions:
* brace expansion
* tilde expansion
* parameter and variable expansion
* command substitution
* arithmetic expansion
* word splitting
* filename expansion
Now here is what i am going to do,go through all of these and actually see if they work as they should.
First lets check the first two : brace expansion and tilde expansion

Why gmail sucks

Bash Rules - Wed, 05/04/2006 - 1:37am
Have a look at its Received Header in More Options , Show Original

Received: by with SMTP id n15csd722wxb;
Thu, 9 Mar 2006 00:46:04 -0800 (PST)

Now why would I care for a stupid mail server's internal i.p?

Firefox cookie policy

Bash Rules - Tue, 04/04/2006 - 11:25pm
Firefox (1.5,1.0.7) lets you set a domain level cookies for top level domain for example
IE blocks these cookies but firefox allows them,so which one is better ? :)

Key findings

Bash Rules - Fri, 24/03/2006 - 12:11am
Haven't blogged bash for a while but then BASH is good :)

Problem: Application A has many config files ( only mutiple lines of key=value type), so one big config file is created merging all the ones(in a very randomized fashion). Now how do you verify that the merged configuration file has not missed out anything.

Solution: A single line of bash code :)

for i in small* ; do while read new; do grep -q ${new%=*} big.cfg || echo ${new%=*} of file $i not found in big.cfg ; done < $i ;done

Formula One

Bash Rules - Mon, 20/03/2006 - 4:29am

Spent this weekend in KL (Kuala lumpur) with some friends from Thoughtworks ..Gabbar..Dator..S2m8kh..Little Chaman..Superman. Had great fun and in the process learnt a thing or two about F1..which I intend to unlearn soon :)...Dator looks crestfallen after Kimi's exit :)

List assignment in scalar/boolean context in perl

Bash Rules - Thu, 22/09/2005 - 3:01am
Though this one is about perl but as i havent blogged for sometime
I'll post it here.

A collegue of mine was using a contruct like

while(my $temp = $sth->fetchrow_array)
# do work..


The problem was that whenever fetchrow_array returned 0 as
the value ,the while would exit out.

So the solution suggested by another colleague was to enclose $temp in a list
such as (my $temp)=$sth->fetchrow_array.

I was intrigued by this as a list should return the last element in scalar

Removing newlines from a file using sed

Bash Rules - Wed, 17/08/2005 - 1:01am
I kept forgetting this as I never really realized that sed is a line editor.
So the newline at the end of the line isnt available by default unless we create a multiline pattern space using N.

Anyways here is the sed command that does the magic.

$sed ':a;N;$!ba;s/\n//g' filename

Among other things it uses labels and N command. :)

Python regex to match a floating point number

Bash Rules - Fri, 12/08/2005 - 2:24am
Here is the expression to match a float upto two places of decimal.
((?<\.|\d)\d+ (?:\.\d{1,2})?)So let me explain it in steps:

'(?<!..)' is a negative look behind,i.e,at that position the preceding text should not match the regex enclosed in '(?<! )'.In this case it is '\.|\d' which stands for a literal dot or a digit.This is not part of the match,the regex that needs to match starts with '\d+'.

Bash arrays

Bash Rules - Fri, 12/08/2005 - 2:22am
First off inside bash arrays ,elements are separated by space.
Have a look at this.

positions=(0 1 2)
echo "${positions[*]}"

0 1 4

Point to note is that 'length' as well as '$length' areboth valid indexes.

bre,ere and pcre

Bash Rules - Thu, 11/08/2005 - 2:41am
Its been a while since I had a look at "Mastering Regular Expressions" by Jeff Friedl but I am glad i went through it once.

So the input is like


So my first attempt at extracting the url was

$cat output| sed 's/^.*filename=//'

Limits of sed

Bash Rules - Wed, 10/08/2005 - 7:18am
Well a user had a huge ascii file 1.5 gigs and all of that on a single line.Wow!

So the job was to make it a span many lines,by putting a newline after every 310 characters.

So the suggested sed expression was:

$sed 's/\(.\{310\}\)/\1\n/' filename

But it appears that sed crapped out with following error:

sed: Couldn't re-allocate memory

Maybe something like C is better suited for this as sed is basically a line editor,so maybe tries to read and operate on it in a single go :).

A bit of sed and awk magic

Bash Rules - Wed, 10/08/2005 - 2:27am
The task is to find the total cpu utilization of httpd processes.
Instead of doing all kinds of acrobatics with grep,bc and bash loops , one can simply use awk.

$ps aux | awk '/httpd/{s+=$3}END{print s}'

One a side note , gnu sed can change a file in-place .This is done by using the "i" option.

$sed -i 's/fun/bun/' test.txt

Disable enter key in bash

Bash Rules - Thu, 04/08/2005 - 2:11am
Well this is very straightforward, we just need to unbind keybinding for
"\r".Thats easily done using bind.

bind -r '\r'

Now to get the binding back i used.

bind '"\r":accept-line'

Obviously i had to hit Ctrl-J instead of "Enter" :).


Bash Rules - Thu, 28/07/2005 - 6:29am
Well i just bumped into this article (
searching for aynchronous jobs.
As my brain is a non starter i dont understand some terms like asynchronous.
So now to keep things simple i feel an asynchronous job is a background job as it can end anytime :).

According to the article "set -b" and "set +b" both of them have issues.
set -b will just corrupt your display and "set +b" can create lot of jobs.Dont ask me why :).

hash builtin - 2

Bash Rules - Thu, 28/07/2005 - 3:29am
Okay i am back to the hash builtin again.Lets trace phash_flush in hashcmd.c.
As expected it calls hash_flush.A look at the top of the function shows what it does,removes and discards
all entries in TABLE.If FREE_DATA is non-null then its kind of a destructor else call free() to free up the memory.

First it is checked if (table == 0) or if number of entries in the table are zero.

Once we are sure that number of hash entries are not zero, we iterate through the hash tables buckets.


Bash Rules - Wed, 27/07/2005 - 10:36am
There was this patch on the bash mailing about making bash run faster on cygwin.

It was pointed out that there were issues with signal handling after the patch is applied.
After the function in the patch is called SIGINT handler is getting lost making the shell to exit instead of just cancelling the current line.

A quick look at the patch shows the following code.

+ sigdelset(&child_sig_mask, SIGTSTP);
+ sigdelset(&child_sig_mask, SIGTTIN);
+ sigdelset(&child_sig_mask, SIGTTOU);

hash builtin

Bash Rules - Wed, 27/07/2005 - 6:28am
I think my brain will finally be a non starter.Anyhow here are my thoughts on the hash builtin of bash.

So i browsed haslib.h where HASH_TABLE is defined as

typedef struct hash_table {
BUCKET_CONTENTS **bucket_array; /* Where the data is kept. */
int nbuckets; /* How many buckets does this table have. */
int nentries; /* How many entries does this table have. */

Obviously the next question would be what is BUCKET_CONTENTS,remember a hash has buckets..


Subscribe to Bash Rules