4

I am on Linux Mint 19.03

First of all, I can assure you that I have read most of the possible questions you might think this question of mine is a duplicate of.

Now, I basically want to type something in my terminal window to open a new terminal window and execute the commands.

Something like this:

[the part I am asking of] "echo $PATH; read"

This code should do open a new terminal, the $PATH variable should be displayed and read is just for halting the terminal.

I tried x-terminal-emulator -e or x-terminal-emulator -c or -x but I could never achieve to do this correctly. All answers on this SE on the similar questions are both old answers and were using -e or -x but it says that those options are deprecated.

So, what is the most proper way of achieving this?

Thanks.

11
  • My answer is not using -e or -x ;-). The problem with -e in gnome-terminal and alike is that they don't pass the (single!) argument to a shell via -c but are actually parsing it themselves, and doing it BADLY. That's why the accepted answer works, because it's using a real shell. x-terminal-emulator -- sh -c 'echo $PATH; read' would've worked, too, with less quote clutter. Commented Apr 12, 2020 at 23:20
  • Notice that -e is still the only way you could pass a command to terminal emulators like xterm or mlterm (and even those have different, strange ideas of what the arguments after -e mean), so there's no general solution. Commented Apr 12, 2020 at 23:25
  • @mosvy Thanks for the help. However, first, your answer scares me(a real beginner :d) and second, your suggestion does not work, a new terminal pops up but nothing is printed there, just like opening a new terminal, just like typing just x-terminal-emulator Commented Apr 12, 2020 at 23:25
  • 1
    It seems that x-terminal-emulator (which is a perl wrapper), does not support the gnome-terminal -- cmd and args form which is "recommended" new way. Your first attempt failed for the same reason as my first suggestion -- that wrapper tries to be smart and turns the -e cmd into sh -c cmd, but the default sh on your system doesn't support read without an argument. If you used read f in the 1st place, your attempt would've worked. Commented Apr 12, 2020 at 23:54
  • 1
    To summarize, either x-terminal-emulator -e bash -c '...' or gnome-terminal -- bash -c '...' will work, but x-terminal-emulator -e '...' or gnome-terminal -e '...' will get you into trouble if the '...' is anything but a simple program arguments... command. Commented Apr 12, 2020 at 23:59

1 Answer 1

4

x-terminal-emulator doesn't start a shell by itself. This leafs just executables to be started with the -e option.

While echo is available as an executable (/bin/echo), read as a bash internal command will fail without bash. Therefore the output in the new window is done faster than it takes to open the window and as read fails, the window is closed before you see it.

This will do the trick:

x-terminal-emulator -e "bash -c 'echo $PATH; read'"

Now x-terminal-emulator starts a bash shell which then will execute echo $PATH; read. As echo and specially read now are available as bash internal commands, the read command will not fail and wait for an input, which keeps the window open until a key is pressed.

6
  • 1
    Note that with the combination of single and double quotes that you use, the $HOME variable will be expanded before x-terminal-emulator is executed. Commented Apr 12, 2020 at 21:50
  • @Kusalananda did you mean the $PATH variable? Commented Apr 12, 2020 at 21:56
  • Duh, yes, sorry. But I don't know if it actually matters or not when it is expanded, unless they need to compare its value with what it is in the current shell. Commented Apr 12, 2020 at 21:58
  • thanks! This works as desired. A bit detailed explanation on how it works would make this answer much better, though. Commented Apr 12, 2020 at 22:03
  • 1
    correct. x-terminal-emulator -e "vi /tmp/file.txt" will start vi in a new window and open /tmp/file.txt to be created or edited. Commented Apr 12, 2020 at 22:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.