Running a command in a new Mac OS X terminal window

I tried to figure out how to run the bash command in a new Max OS X Terminal.app window. Like, for example, here, how I will 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 need something like:

 Terminal.app -c "my command here" 

But of course this will not work. I know the command "open -a Terminal.app", but I don’t see how to redirect the arguments to the terminal, or even if I used all the arguments.

Thank!

+84
bash terminal macos
Jun 12 '09 at 22:26
source share
10 answers

At one point, I might think to do it from my mind, create a .command file and run it like this:

 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 will either have to escape double quotes or not use single quotes

+86
Jun 12 '09 at 22:28
source share

Partial Solution:

Put the things you want to do in a shell script like

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

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

 open -a Terminal.app scriptfile 

and it will be launched in a new window. Add ' bash ' to the end of the script to save the output of a new session. (Although you may have to figure out how to load custom rc files and stuff ..)

+63
Jun 12 '09 at 23:03
source share

I tried 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 
+32
May 21 '10 at 2:34
source share

In case someone cares, here is the iTerm equivalent:

 #!/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 
+8
Oct 23 '10 at 15:00
source share

I made a functional version of Oscar's answer, this also copies the environment and changes to the corresponding directory

 function new_window { TMP_FILE=$(mktemp "/tmp/command.XXXXXX") 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 as follows:

 new_window my command here 

or

 new_window ssh example.com 
+3
Apr 21 '15 at
source share

Here's another take 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

+2
Jun 14 '09 at 8:47
source share

Here is my awesome script, it creates a new terminal window, if necessary, and switches to the Finder directory if Finder is the front-most. It has all the mechanisms necessary 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 text item delimiters set AppleScript text item delimiters to "'" set the parsedList to every text item of theString set AppleScript text item delimiters to "'\\''" set theString to the parsedList as string set AppleScript text item delimiters to oldDelims return "'" & theString & "'" end quote_for_bash 
+2
May 08 '12 at 14:12
source share

You can also call the function of a new terminal command by pressing the key combination Shift + ⌘ + N The command entered in the field will be launched in a new terminal window.

+1
Jun 14 '09 at 8:55
source share

I call it script trun. I suggest putting it in a directory on your executable path. Make sure that he does the following:

 chmod +x ~/bin/trun 

Then you can run the commands in a new window by simply adding trun in front of them, for example:

 trun tail -f /var/log/system.log 

Here is the script. It does some bizarre things, such as passing your arguments, changing the title bar, clearing the screen to remove the mess of shell startup, deleting its file when it is executed. Using a unique file for each new window, it can be used to simultaneously create multiple windows.

 #!/bin/bash # make this file executable with chmod +x trun # create a unique file in /tmp trun_cmd=`mktemp` # make it cd back to where we are now echo "cd `pwd`" >$trun_cmd # make the title bar contain the command being run echo 'echo -n -e "\033]0;'$*'\007"' >>$trun_cmd # clear window echo clear >>$trun_cmd # the shell command to execute echo $* >>$trun_cmd # make the command remove itself echo rm $trun_cmd >>$trun_cmd # make the file executable chmod +x $trun_cmd # open it in Terminal to run it in a new Terminal window open -b com.apple.terminal $trun_cmd 
-one
Jun 03 '17 at 9:48 on
source share
 open `which $command` 

works for simple cases.

-one
Sep 21 '17 at 15:23
source share



All Articles