15

I am new to linux shell scripting. I want to write a shell script which will open terminal with multiple tabs; it should run rtsp client app in each tab.

For this, I have gone through question here in this forum and tried to code like bellow,

tab="--tab-with-profile=Default -e "
cmd="java RunRTSPClient"
for i in 1 2 3 4 5
   do
#   
   foo="$foo $tab $cmd"         
   done
gnome-terminal $foo
exit 0

This is running and opens the terminal window with tabs but suddenly it will close. I am not getting any errors.

12

Use this variant of the script to do what you want:

#!/bin/bash

tab="--tab-with-profile=Default"
cmd="bash -c 'java RunRTSPClient';bash"
foo=""

for i in 1 2 3 4 5; do
      foo+=($tab -e "$cmd")         
done

gnome-terminal "${foo[@]}"

exit 0

Generally, a script like this:

#!/bin/bash

tab="--tab"
cmd="bash -c '<command-line_or_script>';bash"
foo=""

for i in 1 2 ... n; do
      foo+=($tab -e "$cmd")         
done

gnome-terminal "${foo[@]}"

exit 0

will open a new terminal with n tabs executing the <command-line_or_script> in each tab. This can be very useful when you want for example to open a terminal with some tabs with the interpreter at a specific path (using cd /path in the above script).

Also, read man bash, this post and this post to understand the changes.

I have tested these scripts and they work.

  • Same as i told It just displays and closes and no error also getting. How to fix this issue?. – user172001 Jul 2 '13 at 13:18
  • ok now its showing the windows with tabs but not running the app in any tab. and also explain me a bit, how this has fixed the window display issue. – user172001 Jul 2 '13 at 13:22
  • @user172001 Now it's working, check the new edits! – Radu Rădeanu Jul 2 '13 at 18:34
  • this is pretty cool. +1 – Nishant Nov 8 '13 at 10:59
  • 1
    I tried adding cmd1 && cmd2 into the command string to run multiple commands. It only seems to run the first one. Am I doing something blatantly wrong? – byxor Nov 5 '16 at 12:12
6

I came up with my own answer. I think this is a better approach because:

  1. I can understand it. I'm not a bash expert and no explanation was given in the more popular answer for what is ..., ${}, -e, or @
  2. It allows you to easily customize the title and command for each tab
  3. Did I say it's a lot easier to understand?

Note that the ; $SHELL at the end of each gnome-terminal command is what keeps the terminal window open. Otherwise it would immediately close.

title1="tab 1"
title2="tab 2"
title3="tab 3"

cmd1="cd /etc"
cmd2="cd ~/Documents"
cmd3="cd /usr/local"

gnome-terminal --tab --title="$title1" --command="bash -c '$cmd1; $SHELL'" \
               --tab --title="$title2" --command="bash -c '$cmd2; $SHELL'" \
               --tab --title="$title3" --command="bash -c '$cmd3; $SHELL'" 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.