Help Save Code2care! 😢

I've lost 99% of my revenue to AdBlockers & AI. Your support could be the lifeline that keeps this passion project alive!

Buy Code2care a Coffee QR Code

Scan to Buy Me A Coffee and help me continue coding for you!

Bash: Allow Command to Fail without exiting Script


If you are making use of bash command in your script and do not want to exit the script and continue the execution, in such case you can suffix the below code to your command.

command || :

You read this as "The command or || no-operation :". By doing so the command always returns a zero (success) exit status.

Example:
rm "data.csv" || :

Here the rm will not fail if the file data.csv is not found and the flow of your bash script continues.

"The command fails without causing the script to exit"

Bash Allow Command to Fail without exiting Bash Script

You can download this article in various formats for your convenience. Choose from the options below:

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has a Masters Degree in Computer Science with over 15+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright Code2care © 2024 | Privacy Policy | About Us | Contact Us | Search