Start a new terminal session in Bash
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 $SHELL
serves 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.
Related Articles
Check if input parameter exists in Bash
Publish Date:2025/03/21 Views:191 Category:OPERATING SYSTEM
-
When we create a Bash script, we may want to use parameters in our script to run successfully. Therefore, we need to create a script to check the number of input parameters used by the user in the script. All of this prevents unexpected beh
Checking Syntax in Bash
Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM
-
This article discusses methods for checking Bash scripts for syntax problems without running the script. Syntax errors are caused by some grammatical or spelling mistakes in the code. In modern compilers of other programming languages, the
Run batch (.bat) files in CMD
Publish Date:2025/03/20 Views:155 Category:OPERATING SYSTEM
-
This article will show you how to use CMD to run a batch file.bat. There are three ways to run a batch file. Let us discuss them in the following sections. Run batch (.bat) files in CMD by directly clicking on them This way you can just go
Changing CMD text color using batch script
Publish Date:2025/03/20 Views:172 Category:OPERATING SYSTEM
-
This article will first discuss the basic concepts of batch scripts or batch files. After introducing the Batch script, let's discuss how to use the Batch script to change the text color of CMD every second. Batch script or file A batch scr
Batch set command timeout
Publish Date:2025/03/20 Views:50 Category:OPERATING SYSTEM
-
This article will first discuss the concept of timeout command in batch scripting. After that, we will discuss setting timeout command for any other command. Timeout command in batch script A timeout is a utility that pauses or delays for a
Batch merge XML files
Publish Date:2025/03/20 Views:101 Category:OPERATING SYSTEM
-
This article will first discuss and understand the XML file format. After that, we will discuss merging two or more XML files into one file using batch commands and scripts. XML File XML, also known as Extensible Markup Language, is a marku
Run .exe file from command prompt using batch script
Publish Date:2025/03/20 Views:77 Category:OPERATING SYSTEM
-
This article will show you how to use a batch (.bat) script to run a .exe type file. You can use two different commands to achieve this. Let’s discuss each method in the following sections. Run .exe file from command prompt using title an
Deleting files older than N days using batch script
Publish Date:2025/03/20 Views:57 Category:OPERATING SYSTEM
-
In this article, we will use a batch script to delete files older than N days. Deleting files older than N days using batch script The general format of the code to perform this task is shown below. FORFILES /p "D:\DIRECTORY" /S /M *.* /D -
Deleting files using batch script
Publish Date:2025/03/20 Views:178 Category:OPERATING SYSTEM
-
This article will demonstrate how to delete files using a batch script. Deleting files using batch script Generally, we can easily delete files by clicking on delete or pressing the delete button on the keyboard. But in Batch, we have to fo