user544079
user544079

Reputation: 16629

Open multiple terminal windows mac using bash script

#!/bin/bash
#!/usr/bin/python

read -p "Execute script:(y/n) " response
if [ "$response" = "y" ]; then
    echo -e "\n\nLoading....\n\n"

    for ((x = 0; x<5; x++))
    do
        echo -e "Open $x terminal\n\n"
        open -a Terminal.app
    done
fi

This only opens a single new terminal window. How can I open 10 new terminal windows?

Upvotes: 1

Views: 6951

Answers (2)

user3439894
user3439894

Reputation: 7555

If you want to open 10 new Terminal windows from a bash script (or from the command line), use the following command:

osascript -e 'tell application "Terminal"' -e 'repeat 10 times' -e 'do script ""' -e 'end repeat' -e 'end tell'

Or integrated into your existing code, albeit re-coded:

#!/bin/bash

shopt -s nocasematch
read -p " Execute script? (y/n): " response
if [[ $response == y ]]; then
    printf " Loading....\\n"
    for ((x = 0; x<10; x++)); do
        printf " Open %s Terminal\\n" $x
        osascript -e 'tell application "Terminal" to do script ""' >/dev/null
    done
fi
shopt -u nocasematch

Its output would be:

$ ./codetest
 Execute script? (y/n): y
 Loading....
 Open 0 Terminal
 Open 1 Terminal
 Open 2 Terminal
 Open 3 Terminal
 Open 4 Terminal
 Open 5 Terminal
 Open 6 Terminal
 Open 7 Terminal
 Open 8 Terminal
 Open 9 Terminal
$ 

With 10 new Terminal windows showing, assuming you've not set to have new windows open in tabs.

Upvotes: 3

eric.v
eric.v

Reputation: 615

You need to give the -n option to the open command

-n Open a new instance of the application(s) even if one is already running.

for ((x = 0; x<5; x++)) do
    echo -e "Open $x terminal\n\n"
    open -na Terminal.app
done

Upvotes: 0

Related Questions