Why doesn't using two cd commands in bash script execute the second command?
Solution 1:
The culprits are your exec bash
statements in some of your functions.
The exec
statement is a bit weird and not easily understood in the first place.
It means: execute the following command instead of the currently running
command/shell/script from here on. That is: it replaces the current shell
script (in your case) with an instance of bash
and it never returns.
You can try this out with a shell and issue
exec sleep 5
This will replace your current shell (the bash
) with the command sleep 5
and when that command returns (after 5 seconds) your window will close because
the shell has been replaced with sleep 5
.
Same with your script: If you put exec something
into your script, the script
gets replaced with something
and when that something
stops execution, the
whole script stops.
Simply dropping the exec bash
statements should do.
Solution 2:
From help exec
:
exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...] Replace the shell with the given command. Execute COMMAND, replacing this shell with the specified program. ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified, any redirections take effect in the current shell.
The key word here is replace - if you exec bash
from inside a script, no further script execution can occur.