JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Start a new terminal session in Bash

Author:JIYIK Last Updated:2025/03/21 Views:

In various situations when using Bash or another shell, you may need to run a script or program in a new terminal instance or in another tab in the same terminal. Opening a new terminal instance or tab from within Terminal is simple; we'll explain it in enough detail with enough examples.


Use the gnome-terminal command to start a new terminal session in Bash

You have to start a new terminal from an already running instance using the simple command gnome-terminal. This will start a new terminal instance with a new window opened.

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

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

The command above will run the desired program in a new terminal; it is important to provide the full path to the program you want to run.

at the end of the command &moves this task to the background of the original terminal instance; not adding it at the end of the command &may cause errors because 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 of 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 represent the names of the first and second commands respectively.

The $SHELL at the end of the command keeps the terminal open even after the command is completed.

An alternative syntax for the above example is:

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

The exec bash at the end of this command $SHELLserves the same purpose as (i.e., keeps the terminal open after executing the command).


Open a new tab in the same terminal in Bash

Sometimes you may not want to open multiple terminal windows because they are hard to keep track of. In this case, opening a new tab in the same terminal is a better approach.

The command to open a new terminal tab is:

gnome-terminal --tab

This can be used in conjunction with the examples discussed above to execute commands in a new terminal tab, for example:

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

It should be noted 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 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.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial