How do you invoke a terminal through a command and cd to a directory?

南楼画角 提交于 2019-12-12 06:10:46

问题


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.



来源:https://stackoverflow.com/questions/5577222/how-do-you-invoke-a-terminal-through-a-command-and-cd-to-a-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!