Bash tips

This & That

Refer to 'that last thing', and 'the first thing':

1fortune -l > file1
2cat !$  | tr -d u > file2
3cat file1 !$
4diff !^ !$

NB: this can go wrong:

1ls -l file1 file2
2cat !^

NB: this only works when running bash interactively, never in scripts.

Lists

You can put a list inside any bash argument, and bash will expand that part into a new argument.

1echo file {one,two,three}.txt
2echo file-{one,two,three}.txt
3touch !$

Look at text files:

1ls *.{txt,md}

Look at size of jpg or png files in the img/ directory:

1du img/*.{jpg,png}

Automatic Lists

1echo {a..d}
2echo Archive_{B..E}

Using multiple lists works fine.

1mkdir first second third
2echo {first,second,third}/file_{1..3}.txt
3x={first,second,third}/file_{1..3}.txt
4echo $x
5echo {first,second,third}/file_{1..3}.txt
6x="$(!!)"
7echo $x
8for file in $x ; do fortune > $file ; done