Bash – difference between how two ampersands and a semi-colon operate in bash

bash

If I wanted to run two separate commands on one line, I could do this:

cd /home; ls -al

or this:

cd /home && ls -al

And I get the same results. However, what is going on in the background with these two methods? What is the functional difference between them?

Best Answer

The ; just separates one command from another. The && says only run the following command if the previous was successful

cd /home; ls -al

This will cd /home and even if the cd command fails (/home doesn't exist, you don't have permission to traverse it, etc.), it will run ls -al.

cd /home && ls -al

This will only run the ls -al if the cd /home was successful.