Okay, so I was messing around with my console today, and I had this idea to chain a bunch of commands together. You know, like those long strings of commands you see sometimes, where one thing happens, then another, and another, all in one go? I wanted to see if I could do that, and what would happen.
I started simple. I just wanted to list the files in a directory, and then print a message. Easy enough, right? So I typed in something like:

ls ; echo "Files listed!"
And boom, it worked! I saw my file list, followed by the message. It’s like the console executed the first command (ls
), and then, regardless of whether that succeeded or failed, it went ahead and executed the second command (echo
).
Getting a Bit More Complicated
Then I got to thinking, what if I wanted one command to only run if the previous one was successful? That’s where comes in. It’s like saying “do this, and if that works, do this other thing.” So I tried this:
mkdir test_directory && cd test_directory
What I thought would happen is that it’d create a new directory called “test_directory”, and then, only if that worked without any errors, it’d change my current directory into that new one. And yep, that’s exactly what happened! I was in the new directory, ready to go.
Handling Failures
But what about the opposite? What if I wanted something to happen only if a command failed? That’s where comes in. It’s the “or” operator. So I came up with this:

cd non_existent_directory echo "Oops, that directory doesn't exist!"
I knew that “non_existent_directory” didn’t exist, of course. So the cd
command failed, and because it failed, the echo
command kicked in and printed my message. Pretty neat, huh?
Putting It All Together
Finally, I wanted to see if I could combine all of these. So I created this little chain:
mkdir my_new_directory
cd my_new_directory && echo "Successfully changed directory!"
cd non_existent echo "Couldn't change directory."
So First It made the directory. And, because that worked, the second set of commands were used. But the first part of that command, cd non_existent
, failed. Then the last part of that whole chain ran, and it printed “Couldn’t change directory.”. It’s a bit convoluted, but it shows how you can chain things together, with successes and failures triggering different actions.
It is pretty cool when you finally get this chained command working the way you imagine. This can save you a lot of time, instead of typing each command individually.