I'm trying to create a .command
file which will open a terminal window with predefined Environment Variables (Including PATH
).
I tried this:
#!/bin/bash
# Adding CMake to Path
export PATH=$PATH:/Users/Shared/CMake/CMake.app/Contents/bin/:
# Adding Ninja to Path
export PATH=$PATH:/Users/Shared/Ninja/:
# Adding GCC to Path
export PATH=$PATH:/usr/local/gcc-8.2/bin/:
echo Path Updated
Yet when I double click on Finder I get this:
Path Updated
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
Deleting expired sessions...none found.
[Process completed]
Namely it is gone.
Is the a way to have a file which does the following (Maybe it has to be 2 different files, I don't know):
- If clicked from finder will open a new terminal window with all the variables defined / updated (Including the
PATH
). - If run from terminal will update the current terminal state.
Any idea?
Best Answer
You need to explicitly start an interactive
bash
shell at the end of your script in order to keep the window open when you open the.command
file from Finder.The following revision of your script demonstrates that and also streamlines other aspects of your code:
This
.command
file will also from an existing terminal window, but note that you will enter an interactive child shell -exit
ing from that child shell will return to you to the original one.It is possible to amend your script so that if you invoke it from an existing terminal window (shell), it modifies that shell's environment directly, but you then have to
source
/.
the script on invocation (e.g.,. ./script.command
):