Aided by @nickguletskii's answer, and my own comment under his answer, and inspired by @grabantot's upvote of my comment, here's my preferred way to do it, especially when I want the terminal to stay open so I can then manually use it.
Ex. usage: this is really useful to add to your startup programs so this script will run, open a terminal, create and name a tab in the terminal, and run a command for you. Or, you can just add a symlink to this script to your desktop. I use this type of approach so I can double-click a single icon on my desktop and have it open up a bunch of terminals (with various tabs named according to what work I'm going to do in them) and programs to set up my programming environment, for instance, for daily work.
Here's a contrived example, which opens up a single tab, titles it "test", then runs the simple command
cd /etc; ls inside it. The
$SHELL part at the end forces the shell to stay open so you can then see its output and continue using it (I learned this somewhere else on Stack Overflow):
gnome-terminal --tab --title="test" --command="bash -c 'cd /etc; ls; $SHELL'"
Here's a more complicated example which opens up 3 separate tabs in the same gnome-terminal. This is exactly the type of thing my desktop shortcut does so I can open up a bunch of programming windows at once:
gnome-terminal --tab --title="tab 1" --command="bash -c 'cd /etc; ls; $SHELL'" --tab --title="tab 2" --command="bash -c 'cd ~; ls; $SHELL'" --tab --title="tab 3" --command="bash -c 'cd ~/temp3; ls; $SHELL'"
Here's a breakdown of that command above:
gnome-terminal = open up a gnome-terminal
--tab = open up a unique tab for what comes next
--title="tab 1" = title this tab "tab 1"
--command="bash -c 'cd /etc; ls; $SHELL'" = run the
bash -c 'cd /etc; ls; $SHELL' command, which is a command I just made up as an example; here's what it does:
bash -c says it is a bash 'c'ommand
cd /etc = 'c'hange 'd'irectory into the "/etc" path
ls = 'l'i's't contents of this directory
$SHELL = this cryptic tidbit is required to keep the shell open so you can work with it. If you want the shell to open, run your command, then close, simply remove this part. I, however, want the tab to stay open so I can go make programming magic. :)
- we then start back over at the
--tab part to produce tab 2, then again for tab 3. Customize to your heart's content.