views:

2070

answers:

7

Hi,

I've been trying to figure out how to run a bash command in a new Max OS X Terminal.app window. As, an example, here's how I would run my command in a new bash process:

bash -c "my command here"

But this reuses the existing terminal window instead of creating a new one. I want something like:

Terminal.app -c "my command here"

But of course this doesn't work. I am aware of the "open -a Terminal.app" command, but I don't see how to forward arguments to the terminal, or even if I did what arguments to use.

Thanks!

+6  A: 

one way I can think to do it off the top of my head is to create a .command file and run it like so:

echo echo hello > sayhi.command; chmod +x sayhi.command; open sayhi.command

or use applescript:

osascript -e 'tell application "Terminal" to do script "echo hello"'

although you'll either have to escape a lot of double quotes or not be able to use single quotes

cobbal
I've decided on the first method. It's pretty hack-ish, but it works and I don't have to worry about escaping quotes in my command or anything. Thanks.
Walt D
+1  A: 

Partial solution:

Put the things you want done in a shell-script, like so

#!/bin/bash
ls
echo "yey!"

And don't forget to 'chmod +x file' to make it executable. Then you can

open -a Terminal.app scriptfile

and it will run in a new window. Add 'bash' at the end of the script to keep the new session from exiting. (Although you might have to figure out how to load the users rc-files and stuff..)

0scar
+1  A: 

Here's yet another take on it (also using AppleScript):

function newincmd() { 
   declare args 
   # escape single & double quotes 
   args="${@//\'/\'}" 
   args="${args//\"/\\\"}" 
   printf "%s" "${args}" | /usr/bin/pbcopy 
   #printf "%q" "${args}" | /usr/bin/pbcopy 
   /usr/bin/open -a Terminal 
   /usr/bin/osascript -e 'tell application "Terminal" to do script with command "/usr/bin/clear; eval \"$(/usr/bin/pbpaste)\""' 
   return 0 
} 

newincmd ls 

newincmd echo "hello \" world" 
newincmd echo $'hello \' world'

see: codesnippets.joyent.com/posts/show/1516

+1  A: 

You could also invoke the new command feature of Terminal by pressing the Shift + ⌘ + N key combination. The command you put into the box will be run in a new Terminal window.

ayaz
Of course useful to know, but I need to do it programatically. Having my program generate a .command file and then open it is a reasonable (if a bit hackish) solution.
Walt D
+1  A: 

I found this shell script with Google and it works like a charm.

Craig Walker
A: 

I've been trying to do this for a while. Here is a script that changes to the same working directory, runs the command, and closes the terminal window.

#!/bin/sh 
osascript <<END 
tell application "Terminal"
    do script "cd \"`pwd`\";$1;exit"
end tell
END
kimchi
A: 

In case anyone cares, here's an equivalent for iTerm:

#!/bin/sh
osascript <<END
tell application "iTerm"
 tell the first terminal
  launch session "Default Session"
  tell the last session
   write text "cd \"`pwd`\";$1;exit"
  end tell
 end tell
end tell
END
Al Chou