I\'m trying to write a small script to change the current directory to my project directory:
#!/bin/bash
cd /home/tree/projects/java
I save
exec bash
at the endA bash script operates on its current environment or on that of its children, but never on its parent environment.
However, this question often gets asked because one wants to be left at a (new) bash prompt in a certain directory after execution of a bash script from another directory.
If this is the case, simply execute a child bash instance at the end of the script:
#!/usr/bin/env bash
cd /home/tree/projects/java
exec bash
At least with newer versions of bash
, the exec
on the last line is no longer required. Furthermore, the script could be made to work with whatever preferred shell by using the $SHELL
environment variable. This then gives:
#!/usr/bin/env bash
cd desired/directory
$SHELL