51

Is there a way of opening a new terminal from the command line, and running a command on that new terminal (on a Mac)?

e.g., Something like:

Terminal -e ls

where ls is run in the new terminal.

2
  • 2
    I'm trying to figure out how to run a ruby on rails server in a new terminal, using 1 command.
    – zlog
    Aug 17, 2010 at 8:54
  • That sounds reasonable -- I take it you want the stdout to appear in the opened terminal. In that case, yes, you want to write a shell script that does the work you want, and then change its extension to .command, as below. Aug 19, 2010 at 14:00

7 Answers 7

45
osascript -e 'tell app "Terminal"
    do script "echo hello"
end tell'

This opens a new terminal and executes the command "echo hello" inside it.

4
  • 2
    I'm sure this obvious to an applescript expert but how about an example that launches any program? Basically like I can do 'xterm -e somecomnand'
    – gman
    Aug 15, 2012 at 2:30
  • 8
    This can be slightly shorter: osascript -e 'tell app "Terminal" to do script "echo hello"'
    – joemaller
    May 9, 2013 at 4:03
  • 2
    how to set new terminal window top active?
    – Jenkamen
    Aug 11, 2016 at 13:48
  • 2
    And how do I pass along arguments along the lines of "$@" in shell but in osa script?
    – rednoah
    Feb 12, 2019 at 5:33
15

One liners are great

osascript -e 'tell app "Terminal" to do script "cd ~/somewhere"'

Chaining commands is great too

osascript -e 'tell app "Terminal" to do script "cd ~/somewhere &&
ls -al &&
git status -s && 
npm start"'
7

You can do it in a roundabout way:

% cat /tmp/hello.command
#! /bin/sh -
say hello
% chmod +x /tmp/hello.command
% open /tmp/hello.command

Shell scripts which have the extension .command and which are executable, can be double-clicked on to run inside a new Terminal window. The command open, as you probably know, is equivalent to double-clicking on a Finder object, so this procedure ends up running the commands in the script within a new Terminal window.

Slightly twisted, but it does appear to work. I feel sure there must be a more direct route to this (what is it you're actually trying to do?), but it escapes me right now.

4
  • I tried this and a new terminal opens, but then I get: ~$ /Users/username/Work/Dev/scripts/hello.command ; exit; logout [Process completed] This is my hello.command: scripts$ cat hello.command #! /bin/sh - say hello
    – zlog
    Aug 17, 2010 at 8:50
  • Ack, that didn't format out properly, but I think you get the idea.
    – zlog
    Aug 17, 2010 at 8:51
  • @zlog: but it did say 'hello', yes? In Terminal.app's preferences, see the 'Settings' and the 'Shell' tab within that. You'll see a menu 'When the shell exits:'. I think the default is 'Don't close the window', and that's what you're seeing. If you change this to 'Close if the shell exited cleanly', the terminal window should disappear once the shell script finishes. Aug 19, 2010 at 9:32
  • 1
    Can I make command to leave its session open and not log out?
    – Royi
    Sep 1, 2018 at 15:51
6

This works, at least under Mountain Lion. It does initialize an interactive shell each time, although you can replace that after-the-fact by invoking it as "macterm exec your-command". Store this in bin/macterm in your home directory and chmod a+x bin/macterm:

#!/usr/bin/osascript

on run argv
   tell app "Terminal" 
   set AppleScript's text item delimiters to " "
        do script argv as string
        end tell   
end run 
3
#!/usr/bin/env ruby1.9

require 'shellwords'
require 'appscript'

class Terminal
  include Appscript
  attr_reader :terminal, :current_window
  def initialize
    @terminal = app('Terminal')
    @current_window = terminal.windows.first
    yield self
  end

  def tab(dir, command = nil)
    app('System Events').application_processes['Terminal.app'].keystroke('t', :using => :command_down)
    cd_and_run dir, command
  end

  def cd_and_run(dir, command = nil)
    run "clear; cd #{dir.shellescape}"
    run command
  end

  def run(command)
    command = command.shelljoin if command.is_a?(Array)
    if command && !command.empty?
      terminal.do_script(command, :in => current_window.tabs.last)
    end
  end
end

Terminal.new do |t|
  t.tab Dir.pwd, ARGV.length == 1 ? ARGV.first : ARGV
end

You need ruby 1.9 or you will need to add line require 'rubygems' before others requires and don't forget to install gem rb-appscript.

I named this script dt (dup tab), so I can just run dt to open tab in same folder or dt ls to also run there ls command.

3

The top answer worked for me. But in my case (as a script for Raycast), it wasn't bringing the new terminal window to the foreground. I fixed that by adding the "activate" command, like this:

osascript -e 'tell app "Terminal" to activate' -e 'tell app "Terminal" to do script "/your/command"'

I also wanted the shell and window to close automatically afterwards because it was for an interactive program. So I added && exit to the end of the shell command, like this:

osascript -e 'tell app "Terminal" to activate' -e 'tell app "Terminal" to do script "/your/command && exit"'

If you're wanting to close the window automatically like this (and not just exit the shell with a "[Process completed]" message), make sure your Terminal settings have the "Close the window" option set for what to do when the shell exits (under Terminal/Settings.../Profiles/Shell).

0

I would do this with AppleScript. You can streamline it by using the osascript command. Your script would be something like:

tell application "Terminal"
  activate
  tell application "System Events"
    keystroke "t" using {command down}
  end tell
end tell

If you're only going to ever access it in terminal, then you can omit all but the middle tell statement. If you want a new window instead of a new tab, replace the t keystroke with n.

I'm not an experienced enough AppleScripter to know how to get command-line arguments and then retype them in the new window, but I'm sure it's possible and not too difficult.

Also, I think this works and I'm not able to test right now, but I'm pretty sure you can start a shell script with some variant on #!/usr/bin/osascript -e and then save it as an executable however you want. Which, at least in my head, would make it possible for you to type something like $ runinnewterm ls /Applications

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .