1

I'm running a script which, when accepting a specific argument, should change the running directory.

However, despite showing no error at all (and the directory being accessible with regular cd), the directory does not actually change.

What I'm expecting is - if I run the script with the cd argument (for example: potato.sh cd, it should cd to a path set in the script beforehand (ex: /tmp).

This is the code:

if [[ $1 == 'cd' ]]; then
        DIR="/tmp"
        if [[ -n "$DIR" ]]; then
                echo "$DIR" #This is set beforehand, but moved here for the sake of the example
                cd "$DIR"
                if [[ $? == "0" ]]; then
                        echo "Hurray!"
                        exit 0
                else
                        echo "Oh, no!"
                        exit 1
                fi
        else
                echo -e "Could not find ${RED}$DIR${STOP}. Sorry!"
        fi
fi

When run (potato.sh cd), the script will correctly output the directory I set (/tmp), and echo Hurray!, but won't actually change the directory.

Many similar scripts have worked for me before. Would love your advice on what I'm missing...

Thanks!

Cyrus
  • 12,309
dkd6
  • 77

0 Answers0