How do I execute a command in an iTerm window from the command line?

Mark Harrison picture Mark Harrison · Sep 20, 2015 · Viewed 13.7k times · Source

How do I run an iTerm session from the command line, passing in the command which is to be executed in the iTerm window?

The xterm analog is -e, i.e.

xterm -e sleep 10

Answer

SamG picture SamG · Jun 13, 2016

I had found the official documentation, but hadn't thought to wrap the Applescript up with osascript like SushiHangover- very nice. His answer didn't work for me, probably because I'm using the latest beta 3.0 version, so here's one that does work (and also simplifies a bit).

#!/bin/bash
osascript - "$@" <<EOF
on run argv
tell application "iTerm"
    activate
    set new_term to (create window with default profile)
    tell new_term
        tell the current session
            repeat with arg in argv
               write text arg
            end repeat
        end tell
    end tell
end tell
end run
EOF