Running a command in a new Mac OS X Terminal window


Running a command in a new Mac OS X Terminal window



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!




What is com.apple.Dont_Steal_Mac_OS_X

1:



How to find the other point of a Unix domain socket on Mac OS X to write/read it?
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:.
What is the correct directory layout for a web server under Mac OS X?
echo echo hello > sayhi.command; chmod +x sayhi.command; open sayhi.command 
or use applescript:.
How do I programmatically get the shortcut keys reserved by Mac OS X
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.
Fish Interactive Shell full path


Enabling memcache on Mac OSX with stock apache and php5


Offsite backup from a timemachine capsule

2:



Add background image/color to DMG using hdiutil or other tool? [duplicate]
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..).


3:


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 


4:


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 


5:


Here's my awesome script, it creates a new terminal window if needed and switches to the directory Finder is in if Finder is frontmost.

It has all the machinery you need to run commands..
on run     -- Figure out if we want to do the cd (doIt)     -- Figure out what the path is and quote it (myPath)     try         tell application "Finder" to set doIt to frontmost         set myPath to finder_path()         if myPath is equal to "" then             set doIt to false         else             set myPath to quote_for_bash(myPath)         end if     on error         set doIt to false     end try      -- Figure out if we need to open a window     -- If Terminal was not running, one will be opened automatically     tell application "System Events" to set isRunning to (exists process "Terminal")      tell application "Terminal"         -- Open a new window         if isRunning then do script ""         activate         -- cd to the path         if doIt then             -- We need to delay, terminal ignores the second do script otherwise             delay 0.3             do script " cd " & myPath in front window         end if     end tell end run  on finder_path()     try         tell application "Finder" to set the source_folder to (folder of the front window) as alias         set thePath to (POSIX path of the source_folder as string)     on error -- no open folder windows         set thePath to ""     end try      return thePath end finder_path  -- This simply quotes all occurrences of ' and puts the whole thing between 's on quote_for_bash(theString)     set oldDelims to AppleScript's text item delimiters     set AppleScript's text item delimiters to "'"     set the parsedList to every text item of theString     set AppleScript's text item delimiters to "'\\''"     set theString to the parsedList as string     set AppleScript's text item delimiters to oldDelims     return "'" & theString & "'" end quote_for_bash 


6:


I made a function version of Oscar's answer, this one also copies the environment and changes to the appropriate directory.
function new_window {     TMP_FILE="tmp.command"     echo "#!/usr/bin/env bash" > $TMP_FILE      # Copy over environment (including functions), but filter out readonly stuff     set | grep -v "\(BASH_VERSINFO\|EUID\|PPID\|SHELLOPTS\|UID\)" >> $TMP_FILE      # Copy over exported envrionment     export -p >> $TMP_FILE      # Change to directory     echo "cd $(pwd)" >> $TMP_FILE      # Copy over target command line     echo "$@" >> $TMP_FILE      chmod +x "$TMP_FILE"     open -b com.apple.terminal "$TMP_FILE"      sleep .1 # Wait for terminal to start     rm "$TMP_FILE" } 
You can use it like this:.
new_window my command here 
or.
new_window ssh example.com 


7:


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 .


8:


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..



73 out of 100 based on 53 user ratings 1303 reviews