March 3, 2017

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.:


for i in table1 table2 table3 mytable yourtable theirtable


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


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.

Topics: Bioinformatics, Blog, Shell scripting