Random (but amazing) bash shell tricks

This is just a random collection of bash shell tricks I need to collect in one place. I hope you find them useful.

Process Substitution

When a program expects multiple filename arguments, but you don’t have files but streams, “Process Substitution” lets you replace the filename with a command to be executed, and the output of that is fed to the program as a file.

For example, if you wanted to extract the first column from one CSV table, and concatenate it (row-by-row) with the last column from a second CSV table:

paste -d , <( awk -F, '{print $1}' table1.csv) <( -F, awk '{print $NF}' table2.csv)

While loops with read

To process a line at time, this construct can come in handy.

cat | while read CMD ; do
echo $CMD
done

Beware, though, that the commands in the loop are in a sub-shell, and so any variables you set there cannot be accessed later in the script.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s