MacOS – Lauching a Terminal ‘.command’ file, without exiting session

bashcommand linemacosterminal

I've found several similar requests, but not really what I'm looking to find. I want to write a shell script, run its commands via double-click, and stay open, to continue my work in the Terminal.

I often create a standard shell script, and put them in a file, like "myScript.command". This type of file allows me to double-click on it, and run the script inside, but these exit on completion. I want the session to stay open, instead of automatically exiting, so that I can continue from where the script ended. I've seen suggestions to use &, and wrapping with ( ), but that still exits.

I've tried things from this thread How to prevent the application launched in Terminal from undesirably exiting? but it wasn't what I needed.

I'm hoping it's some easy switch, or pipe.

Thanks!

Best Answer

As the final step in your script you can make it run a shell for you - just put the shell as the final line, eg. bash, or zsh.

That'll then drop you into the Terminal, within the script, and you'll get access to it's environment too.

However, that'll also stop your scripts from running to completion if you're not running them interactively. You could test for that and handle it as part of your script. Do it often enough and it might be worth making a shell function for it and sourcing it in from a standard location.

Or, since what you seem to want is just an easy way to run scripts, you can setup a bunch of aliases for them in your shell rc file (.zshrc or .bashrc), and have them available to call when you want them. That way you'll still be able to continue your work, and it's easy to re-run the script too - just run the alias again.