How do you invoke a terminal through a command and

2019-09-03 08:03发布

问题:

I'm trying to invoke a terminal from the command line and cd into a particular directory on my MAC machine. I understand that this can be done using this command on Linux.

gnome-terminal --working-directory="/path/to/new/directory"

Is there an equivalent in Mac without actually writing an AppleScript? I know this command opens a new terminal though.

open -a Terminal.app

All I need to figure out is a way to add an argument to cd into another directory from the new terminal.

Any help is much appreciated. Thanks in advance, dkulkarni

回答1:

As far as I know, there isn't a sensible way to pass Terminal a simple command-line request like this. I agree that's a bit daft, but there you go. The accepted way is via AppleScript. If you really can't stomach that, write out a shell command and open that instead.

Examples of both approaches can be found in this answer. Note that if you're writing a .command file there will be an implicit exit afterwards, so you need to stick an extra shell at the end, something like this:

echo cd /\;bash > /tmp/new.command;chmod +x /tmp/new.command; open /tmp/new.command

And the environment probably won't end up being set up how you want it. The AppleScript method is a bit less hacky and should leave you with the correct environment:

osascript -e 'tell application "Terminal" to do script "cd /"'

Works for me, anyway.