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

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

本文介绍了如何通过命令调用终端和cd到目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从命令行调用终端,并cd到我的MAC机器上的特定目录。我知道这可以在Linux上使用这个命令。

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"

在Mac中有没有实际写入AppleScript的等价物?
我知道这个命令打开一个新的终端。

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

open -a Terminal.app

所有我需要弄清楚的是一个方法来添加一个参数cd到另一个目录从新的终端。

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

任何帮助非常感激。
提前感谢,
dkulkarni

Any help is much appreciated.Thanks in advance,dkulkarni

推荐答案

据我所知,方式传递终端一个简单的命令行请求这样。我同意这有点蹒跚,但你去。接受的方式是通过AppleScript。如果你真的不能忍受,写出一个shell命令和 open

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.

方法可以在。注意,如果你写一个 .command 文件,将会有一个隐式的退出,所以你需要坚持最后一个额外的shell,像这样:

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

并且环境可能不会最终设置如何想要它。 AppleScript方法有点不太好,应该留下正确的环境:

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 /"'

对我来说有用。

这篇关于如何通过命令调用终端和cd到目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:43