News and Blog

Random bash shell tidbits

This is probably common knowledge, but worth repeating since it saves so much time.

1. If you're typing a long list of commands that all end in the same parameter, e.g. a username, you don't need to keep on typing that last parameter out every time. Instead, type !$ in its place and bash will copy the last parameter from the previous command and substitute it for you.

2. If you're copying and pasting chunks of code such as SQL and only changing small sections between each chunk, then rather than spend all that time doing manual search-and-replace, you can use bash to auto-generate the code for you in a for loop. e.g.:

#!/bin/bash

for i in table1 table2 table3 mytable yourtable theirtable

do

echo "update $i set ${i}_foo=1 where ${i}_bar=0;"

done

The above will generate a lovely series of SQL statements that apply the same operation to a list of tables that otherwise you would have had to copy-paste and modify individually for each table involved.

Bioinformatics Blog Shell scripting

About Richard Holland