This bash file running on Mac terminal failed to change the directory. Rather reporting it does not exist when it actually does. Any thing I did wrong? #!/usr/bin/env bashset -eread nameAPPLICATION_PATH=~/Documents/meteor/apps/$namecd ${APPLICATION_PATH}
bash script to cd into directory
bash;shell script;cd command
There are two points: Problems with tilde expansionProblems with sourcing vs. executing.For the tilde part, a very recent question at superuser was about the same issue (https://superuser.com/questions/1161493/why-bash-script-wont-extend-bashrc/1161496#1161496)The tilde is expanded before the variable, so the cd cannot find the path. To overcome this, lead the command with eval as such:eval cd ${APPLICATION_PATH}Unfortunately, when you execute the script (I mean, if it is chmod'ed to +x, calling the path), you will see that the $PWD does not change in the current shell. However if you add such a line at the end of the scriptlsYou will see that, ls is executed at the new working directory. How come?The answer is here (https://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-and-sourcing-a-bash-scrip#176788)Short answer: sourcing will run the commands in the current shell  process. executing will run the commands in a new shell process. still  confused? then please continue reading the long answer.Shortly, to change the $PWD at current shell, you should source the script as such:source /path/to/scriptor. /path/to/scriptA third point: If you don't want to mess with source or ., you can define an alias in your ~/.bashrc (https://stackoverflow.com/questions/752525/run-bash-script-as-source-without-source-command):alias mycmd=source mycmd.sh