Daisy chaining commands - Redux

A while ago, Mat posted a blog entryregarding running multiple commands on the same line.  While queuing up commands like that is an invaluable feature of any shell, what do you do if you want to run commands that depend on each other? As an example, imagine a program that operates on the current working directory, so you want to change to the target directory, then run the script.

cd /var/www/html/mysite/tmp; /usr/bin/my-script

But what if something went wrong? What if the directory didn’t exist or you didn’t have permissions to enter it? Your script would be run, but not in the directory you intended.

Luckily for us, there is ‘&&’. Like ‘;’, ‘&&’ allows you to run multiple commands at once, but it only runs the next command if the current one exited successfully. So instead you could run:

cd /var/www/html/mysite/tmp && /usr/bin/my-script