cd with variable not working [duplicate]

Solution 1:

Executing bash test.sh or ./test.sh creates a child bash process to execute your script, with a separate working directory.

You can instead use source test.sh or the shorter equivalent . test.sh to run the script in the existing bash instance and environment.