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.

muyustan
  • 253
  • 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. –  Apr 12 '20 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. –  Apr 12 '20 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 – muyustan Apr 12 '20 at 23:25
  • There's nothing to be scared of, it won't eat you. My suggestion works with bash instead of sh (which on debian and alike is a different shell, dash, where the read without a variable name is an error) x-terminal-emulator -- bash -c 'echo $PATH; read'. Sorry for that, I should've tested it. –  Apr 12 '20 at 23:29
  • @mosvy actually, I had done that replacement already, but did not change anything. Just tried again after your recent comment, again, not working. Are you able to run it for sure? If so, then I am doing something wrong. – muyustan Apr 12 '20 at 23:36
  • x-terminal-emulator is wrapper. What terminal emulator exactly does it wrap to on your system? Does it still not work if you run it directly as gnome-terminal or mate-terminal? –  Apr 12 '20 at 23:39
  • muyustan@mint:~$ update-alternatives --display x-terminal-emulator x-terminal-emulator - auto mode link best version is /usr/bin/gnome-terminal.wrapper link currently points to /usr/bin/gnome-terminal.wrapper link x-terminal-emulator is /usr/bin/x-terminal-emulator slave x-terminal-emulator.1.gz is /usr/share/man/man1/x-terminal-emulator.1.gz /usr/bin/gnome-terminal.wrapper - priority 40 slave x-terminal-emulator.1.gz: /usr/share/man/man1/gnome-terminal.1.gz – muyustan Apr 12 '20 at 23:45
  • @mosvy I am not sure if it wil be helpful but I shared some update-alternatives output above, it is not the best format to read, however, it looks like it was pointing to gnome-terminal, which I also was thinking so. However, surprisingly, replacing x-terminal-emulator with gnome-terminal, it worked. I did not understand why actually. A more readable output by the way : muyustan@mint:~$ update-alternatives --list x-terminal-emulator --> /usr/bin/gnome-terminal.wrapper – muyustan Apr 12 '20 at 23:47
  • 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. –  Apr 12 '20 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. –  Apr 12 '20 at 23:59
  • @mosvy you are so right, muyustan@mint:~$ sh $ read sh: 1: read: arg count each line seperated by a space. Thanks for all help. You should consider to compile these comments in an answer, it might be useful – muyustan Apr 13 '20 at 00:00

1 Answers1

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.

AdminBee
  • 22,803
bey0nd
  • 937
  • 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. – Kusalananda Apr 12 '20 at 21:50
  • @Kusalananda did you mean the $PATH variable? – bey0nd Apr 12 '20 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. – Kusalananda Apr 12 '20 at 21:58
  • thanks! This works as desired. A bit detailed explanation on how it works would make this answer much better, though. – muyustan Apr 12 '20 at 22:03
  • yes! after your edit, now I understand the need of using bash so, if no bash internal command will be used, then is bash -c part not a must to have? – muyustan Apr 12 '20 at 22:12
  • 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. – bey0nd Apr 12 '20 at 22:15