1
#!/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?

1
  • I think you can only have one shebang (#!) per file, so your #!/usr/bin/python is unnecessary / ignored. Commented Feb 4, 2018 at 10:35

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Or, to integrate closer to the code in question: for ((x=0; x<5; x++)) ; do osascript -e 'tell application "Terminal" to do script "" ' ; done
@Asmus, Didn't see you comment when posted and only saw it now because someone upvoted the answer; however, the OP stated "How can I open 10 new terminal windows?" and why I used x<10 instead of 1x<5 as in his code, so using 1x<5 or integrating closer is not what the OP wanted. 10 is what's wanted.
0

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

3 Comments

open -an Terminal.app gives "Cannot find file" error
Sorry I mixed up the option order. Should be n first then a
Note that this opens additional instances of Terminal.app, not additional windows within one app. The latter should be preferred, I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.