script.sh
is executed in a child process ("subshell"), and changing working directory in a child process doesn't affect working directory of parent process.
To change directory with something that looks like "a script", you have at least 3 possibilities:
- setting an alias; suitable for interactive work with shell; not your case, as far as I understand;
- writing a shell function instead of
script.sh
, see an example here;
- run
script.sh
in the current process using source
or simply .
Example of the last:
source script.sh
or
. script.sh
More on source
command here.
And there is another really weird possibility, see here: change working directory of caller. Nevertheless, don't take it too serious, consider like a very geeky joke for programmers.