简体   繁体   English

如何通过命令调用终端并cd到目录?

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

I'm trying to invoke a terminal from the command line and cd into a particular directory on my MAC machine. 我正在尝试从命令行调用终端,然后将cd插入我的MAC机器上的特定目录。 I understand that this can be done using this command on Linux. 我了解可以在Linux上使用此命令来完成。

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

Is there an equivalent in Mac without actually writing an AppleScript? 在Mac中是否有等效功能而不实际编写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. 我需要弄清楚的是一种从新终端向另一个目录添加cd参数的方法。

Any help is much appreciated. 任何帮助深表感谢。 Thanks in advance, dkulkarni 在此先感谢,dkulkarni

As far as I know, there isn't a sensible way to pass Terminal a simple command-line request like this. 据我所知,没有一种明智的方式将这样的简单命令行请求传递给Terminal。 I agree that's a bit daft, but there you go. 我同意这有点愚蠢,但是您去了。 The accepted way is via AppleScript. 可接受的方式是通过AppleScript。 If you really can't stomach that, write out a shell command and open that instead. 如果您实在无法忍受,写出一个shell命令并open它。

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: 请注意,如果您正在编写.command文件,则此后将隐式exit ,因此您需要在末尾附加一个额外的shell,如下所示:

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: AppleScript方法不那么精巧,应该为您提供正确的环境:

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

Works for me, anyway. 无论如何对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM