How to Start a New Terminal Session in Bash

Faaiq Bilal Feb 02, 2024
  1. Use the gnome-terminal Command to Start a New Terminal Session in Bash
  2. Open a New Tab in the Same Terminal in Bash
How to Start a New Terminal Session in Bash

In various situations, when working with Bash or other shells, a need may arise to run a script or a program in a new terminal instance or maybe another tab in the same terminal. Opening new terminal instances or tabs from within the terminal is simple; we will explain it in detail with sufficient examples.

Note
The article examples are for systems running a GNOME desktop environment which is common in most LINUX distributions.

Use the gnome-terminal Command to Start a New Terminal Session in Bash

You must use the simple command gnome-terminal to start a new terminal from an already running instance. This will start a new terminal instance, and a new window will open.

If you want to open a new terminal and run a program in it from the already running instance, there are a few different ways to accomplish this task.

gnome-terminal -x "complete/path/of/the/program" &

The above command will run the desired program in a new terminal; it’s important to provide the complete path of the program you want to run.

The & at the end of the command moves this task to the background of the original terminal instance; not adding the & at the end of the command might cause errors as the program may try to run before the new terminal has been initialized.

The bash -c option can be used with the gnome-terminal command to execute multiple Bash commands in a new terminal.

The syntax for the gnome-terminal command is as follows.

gnome-terminal --command="bash -c '[cmd1]; [cmd2]; $SHELL'"

Here --command="bash -c" tells the new terminal that these are Bash commands or scripts, and cmd1 and cmd2 stand for the names of the first and second commands, respectively.

The $SHELL at the end of the command makes the terminal stay open even after completing the commands.

An alternative syntax for the example above is:

gnome-terminal -x bash -c "<cmd>; exec bash"

The exec bash at the end of this command serves the same purpose as $SHELL (i.e., keep the terminal open after executing the commands).

Open a New Tab in the Same Terminal in Bash

Sometimes, you may not want to open multiple terminal windows as they can get hard to keep track of. Opening a new tab in the same terminal is a better approach in such situations.

The command to open a new terminal tab is:

gnome-terminal --tab

This can be combined with the examples discussed above to execute commands in a new terminal tab, such as:

gnome-terminal --tab -x bash -c "<cmd>; exec bash"

It is important to note that the above commands are for systems that support the GNOME environment.

For macOS, you can execute a command in a new terminal from an already running terminal by using:

osascript -e 'tell app "Terminal" to do script "cmd"'

This will open a new terminal and execute the cmd command in the newly created terminal.