Dave's Brain

Browse - Programming Tips - How can I check that a cd in a bash script worked?

Date: 2017oct13
Language: bash
Keywords: success, die

Q.  How can I check that a cd in a bash script worked?

A.

if cd /tmp; then echo Worked else echo Failed fi
If you want the script to end when the cd fails
if cd /tmp; then : # This means do nothing on success else echo "Could not change to /tmp" exit 1 fi # Remainder of script

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.