Bash Tricks

This page is for Bash programming tips and tricks, most of which will only be code fragments.

Completed solutions should probably go in Useful One-Liners.

Keeping while in context

From Bob Dunlop

I’ve long been annoyed by the behaviour of Bash and pipelines as exhibited by the following small script.

#!/bin/bash generate() { echo "one" echo "two" echo "three" } last="dummy" generate | while read x do last=$x echo "x= $x last= $last" done echo "last= $last"

Which produces the following output.

x= one last= one x= two last= two x= three last= [continued...]