How to focus parent terminal tab of shell script for input?
This is a little awkward since I can't run this code from within your script (nor within an applescript) but this osascript should allow you to specify a desired tab (tp) and switch to it from any other within the same window.
osascript -e '
tell application &quot;Terminal&quot; to set tp to item 1 of (tabs of window 1 whose selected is true)
tell application &quot;System Events&quot; to tell process &quot;Terminal&quot; to keystroke &quot;t&quot; using command down
delay 1
tell application &quot;Terminal&quot; to set selected of tp to true'

I think your issue was just a syntax one: e.g. selected tab versus tab whose selected is true.
Each line does the following:

Sets variable tp to the active tab
Creates a new tab using system events
(which becomes the active tab)
Delay 1
Makes tp the active tab

The variable tp will resemble this:
--&gt; tab 3 of window id 360 of application &quot;Terminal&quot;

Note, there are some alternatives to setting tp to the active tab but they're more complex.