Linux open terminal with command

How can I open a new terminal window from a cli command and print «Hello World»

I’ve been playing with the gnome-terminal command for some minor automations, and I keep running into the same issue. The goal is to open a new window in the same directory as the current window, and execute a simple command (i.e. echo, cd, or similar). This command seems to work fine to open the new window in the same directory:

DIR=$(pwd); gnome-terminal --working-directory=$DIR 
DIR=$(pwd); gnome-terminal --working-directory=$DIR -e 'echo "test"' 
DIR=$(pwd); gnome-terminal --working-directory=$DIR -- 'echo "test"' 

However I now receive this error: Failed to execute child process “echo «test»” (No such file or directory) Does anyone know why this is happening? Exact System Specs: Ubuntu 18.04, GNOME Terminal 3.32.1 using VTE 0.56.2 +GNUTLS The exact deprecation message is: # Option “-e” is deprecated and might be removed in a later version of gnome-terminal.

# Use “-- ” to terminate the options and put the command line to execute after it. 

What is the exact text of the deprecation warning? Also, what version of gnome-terminal are you using?

Hi barton, thanks for your feedback. I’ve updated the question with the information you asked for. Let me know if there’s anything else I can add.

2 Answers 2

Ah. I see. The two messages are actually distinct.

# Option “-e” is deprecated and might be removed in a later version of gnome-terminal. 

I think this means that you should use -x , instead — everything on the command line after -x is considered to be part of the command to be executed.

# Use “-- ” to terminate the options and put the command line to execute after it. 

This is a convention used in command line programs. The two dashes indicate that anything that comes afterward, regardless of whether or not it has a dash, is not a command line option. It’s useful in conjunction with -x , because you may have stuff that you don’t want parsed as one of the options to gnome-terminal — rather they’re command line arguments to be run inside the terminal.

I think an example would probably make this clearer.

gnome-terminal -x -- grep -v "42" hitchikers_guide_to_the_galaxy.txt 

This opens a gnome terminal. Inside the terminal we’re running grep, and excluding any line that contains the string «42». If we had not used — , gnome-terminal would see -v as a command line argument of gnome-terminal , and probably thrown a warning saying that -v is not a valid argument to gnome-terminal.

Читайте также:  Linux copy drive to drive

Ok, so both -x and -c are deprecated. You can execute commands without any flags. It does require using — to terminate all other options. so I was pretty much wrong about everything that I said. The rationale was solid, just wrong.

will open a new terminal, and launch nano inside the terminal.

Things get tricky when you try

It runs, but it exits so fast that you don’t see anything other than a flash on the screen.

Note that there is also an open bug about the deprecation of -c :

I got really excited reading this as it seems to make a lot of sense the way you describe it, but I still get an error: DIR=$(pwd); gnome-terminal —working-directory=$DIR -x — echo «test» # Option “-x” is deprecated and might be removed in a later version of gnome-terminal. # Use “— ” to terminate the options and put the command line to execute after it. # _g_io_module_get_default: Found default implementation gvfs (GDaemonVfs) for ‘gio-vfs’

To clarify the issues in the OP, 2 Points:

DIR=$(pwd); gnome-terminal --working-directory=$DIR -- 'echo "test"' 

the (unexplained) error indicates that the «command» is

i.e. it is trying to run the program stored in the file named echo «test» (being the single argument after the —). The correct syntax would be

DIR=$(pwd); gnome-terminal --working-directory=$DIR -- echo "test" 

but (as stated by Barton Chittenden) that then results in the terminal window appearing and disappearing (after executing the echo) almost instantly (probably faster than the screen refresh rate, giving the appearance that nothing happened). It needs some delay. So try

DIR=$(pwd); gnome-terminal --working-directory=$DIR -- echo "test" ; sleep 5 

(I know this is wrong, wait for the analysis)

but this just seems to do nothing for longer. Reason:- ; is a reserved symbol (control operator) and terminates the argument list for gnome-terminal . The sleep 5 is a separate command executed after the (do nothing) gnome-terminal . Trying to quote the ; is ineffective:

DIR=$(pwd); gnome-terminal --working-directory=$DIR -- echo "test" \; sleep 5 

This is syntactically valid if what you want is to display test ; sleep 5 in a nanosecond window. No, the problem here is that we are trying to execute a command LIST not a command (single executable). So the answer is:

Читайте также:  Нет сетевого интерфейса linux

    Execute a general command line in a new window using «sh -c»:

DIR=$(pwd); gnome-terminal --working-directory=$DIR -- sh -c 'echo "test" ; sleep 5' 
DIR=$(pwd); gnome-terminal --working-directory=$DIR -- sh -c 'echo "test" ; echo Press ENTER to continue ; read a' 

P.S. For the pedants, I know that spaces around ; and the » around (the single word) test are unnecessary. Also, I have used the generic sh to represent whichever version is in use on the target system (and assumed that -c works as in Bourne shell). Further, the shell variable ( DIR in the example) is redundant, just use —working-directory=$(pwd) , unless you want to use the value again within the subsequent command (line). In fact, in my testing, gnome-terminal starts in the current directory of the invoking process, so —working-directory is almost certainly redundant here. Finally, the question wanted to print «Hello World», so the (fully and minimally) correct answer (with added delay) is:

gnome-terminal -- sh -c 'echo Hello World;sleep 5' 

P.P.S. While testing the (deprecated) -e and -x options, the following was noted (V3.28.2):

    -x has the same effect as — , further argument processing is still inhibited. This possibly explains the «odd» error

#_g_io_module_get_default: Found default implementation gvfs (GDaemonVfs) for ‘gio-vfs’ 

as it is trying to execute the command — . (Didn’t get this exact error in my testing, just the standard «—» No such file or directory .)

  • -e invokes its argument as a command line BUT not as sh(1) input. The argument is parsed into words at white-space boundaries (tested space, tab and newline), allowing \ (quote single character including white-space) and both » and ‘ (quote enclosed string, no observed difference). The first word is the command name. It must be a path to an executable file (absolute, relative or $PATH findable), NOT a shell only built-in (e.g. read , cd vs. shell built-ins that have a corresponding external binary, e.g. echo , sleep ). The command is invoked using the remaining words as arguments (probably using exec(3) since system(3) would recognise shell syntax). -e overrides any other command invocation options, i.e. — , -x .

Источник

How to open Terminal Using Ubuntu Shortcut

Terminal is a Command Line Interface abbreviation of CLI used to exchange information with computer programs and utilizes a GUI (Graphical User Interface) environment to communicate with shell. Some commonly used Linux terminals are gnome-terminal, Konsole and xterm.

Читайте также:  How to install pyqt5 on linux

By sending simple text commands to our computer in the terminal, we can perform various tasks like copying files, creating and removing a directory or file etc. The Terminal provides a more systematic way to access a computer capability more than any GUI app could possibly do.

This write-up is focusing on how to open terminal using shortcut ways.

How to open terminal in Ubuntu using shortcut methods

If you want to open terminal in Ubuntu without any difficulty then follow the procedures mentioned below:

Method 1: Opening terminal using keyboard shortcut

It is the easiest approach to open a terminal in Ubuntu. Press Ctrl+Alt+T on the keyboard, a terminal window will pop-up on your screen as demonstrated below:

Method 2: Opening terminal through “Run a Command” dialogue window

Press Alt+F2 from the keyboard and a dialogue box will be opened. Enter “gnome-terminal” in dialogue box and press Enter from the keyboard:

The above entered command will open the terminal window.

Method 3: Creating custom shortcut key to launch terminal

As Ctrl+Alt+T is default keyboard shortcut to launch terminal but in case you don’t remember or want to change it, follow the procedure mentioned below:

Step 1: Open the Settings panel and click on “Keyboard Shortcuts”:

Step 2: Now under Launchers option find Launch terminal, you can now see the keyboard shortcut used for launching the terminal directly:

Step 3: For convenience shortcut keys can be altered. If you want to change the shortcut key for launching terminal then click on “Launch terminal” and enter a new shortcut by pressing keys and click on set to save the new shortcut key:

Now your new shortcut key will appear against the “Launch terminal” label under Launchers option in the keyboard and shortcuts panel:

Conclusion

Terminal is a Command Line Interface (CLI) which helps the user to communicate with computers by sending text commands to the computer to perform different functions. In this article we discussed three shortcut ways to launch a terminal in Ubuntu. First is keyboard shortcut, second is through Run a Command dialogue window and third through creating a custom shortcut key.

About the author

Alishba Iftikhar

I am currently an undergraduate student in my 1st year. I am an internee author with Linuxhint and loved learning the art of technical content writing from senior authors. I am looking forward to opting my career as a full time Linux writer after I graduate.

Источник

Оцените статью
Adblock
detector