2

How to write commands with default variables for terminal or default app (Browser, Terminal) so it would work on any Ubuntu or Debian?

For example Ubuntu mate has mate-terminal How to write commands that would open in each distro with befault terminal and browser? So in this command line wouldnt be mate-terminal and firefox but some wrapper of them for any distro user default apps of terminal or browser.

mate-terminal -- bash -c 'sleep 2; dpkg -S "\/bin/$(xprop WM_CLASS | cut -d "\"" -f2)"; sleep 5; firefox'

how to find default app variables or wrappers so that command would be running in any distro terminal apps? How to find also default browser or text editor variables?

2
  • 2
    That is theoretically impossible because for any strategy you come up with, someone could create a Ubuntu or Debian based distribution your strategy doesn't work with. Please be more specific about your actual goal. Regarding default applications, take a look at xdg-open and xdg-mime, but those are only really helpful if the distribution in question is (at least to some extend) xdg compliant. – danzel Mar 7 '20 at 15:45
  • 2
    Most of them should set x-terminal-emulator to the distro's default terminal emulator via the update-alternatives mechanism – steeldriver Mar 7 '20 at 15:49
1

If you need to run the same command on multiple platforms / distributions there is a workaround which involves writing customized copies of the same command and separating them with the || operator which will try to execute the next command if the first command fails.

Example

Command for mate-terminal:

mate-terminal -- bash -c 'sleep 2; echo "hi"; sleep 5; firefox'

Command for gnome-terminal:

gnome-terminal -- bash -c 'sleep 2; echo "hi"; sleep 5; firefox'

Command that will run on either terminal:

mate-terminal -- bash -c 'sleep 2; echo "hi"; sleep 5; firefox' || gnome-terminal -- bash -c 'sleep 2; echo "hi"; sleep 5; firefox'

Command that will run on either terminal and launch either firefox, google-chrome or chromium:

mate-terminal -- bash -c 'sleep 2; echo "hi"; sleep 5; firefox || google-chrome || chromium' || gnome-terminal -- bash -c 'sleep 2; echo "hi"; sleep 5; firefox || google-chrome || chromium'

This will however require looking into target platforms to find out how to correctly run the command.

Best of luck

2
  • Command would be used for any distro, but user might have different apps and chrome or chromium and not firefox. – Kangarooo Mar 7 '20 at 16:12
  • @Kangarooo Then you would write a copy of the command for each app chromium, chrome and firefox ... etc and separate them with ||. Otherwise I can't think of other ways right now. – Raffa Mar 7 '20 at 16:16
1

I do not know much about how different distribution set their preferred apps for web browser and terminal emulator, but I do for Xfce desktop environment. Maybe you can do the same with other DEs, if you install exo-utils package.

The preferred applications would be set by running

exo-preferred-applications

and your example command (purpose of which is unknown to me) would be formulated like this:

exo-open --launch TerminalEmulator bash -c 'sleep 2; dpkg -S "\/bin/$(xprop WM_CLASS | cut -d "\"" -f2)"; sleep 5; exo-open --launch WebBrowser; read'

read is needed because the web browser might close with the terminal window.

2
  • Oh, wow. How to find all alternatives? What they are called like? Wrappers of commands? – Kangarooo Mar 8 '20 at 17:25
  • @Kangarooo man exo-open – jarno Mar 9 '20 at 1:18
0

Here is rewrite of your command using Debian's alternatives system:

x-terminal-emulator -e bash -c 'sleep 2; dpkg -S "\/bin/$(xprop WM_CLASS | cut -d "\"" -f2)"; sleep 5; x-www-browser'

Note the use of -e for the command the terminal emulator should run.

If you would like to change the default terminal emulator you would run

sudo update-alternatives --config x-terminal-emulator

Respectively, to change the default web browser you would run

sudo update-alternatives --config x-www-browser
2
  • how to see other wrappers or how to call them? – Kangarooo Mar 8 '20 at 17:22
  • @Kangarooo Type sudo update-alternatives --config (without pressing Enter) in terminal and press Tab key three times to complete. You can call the wrappers by their names since their symbolic links are found in PATH. See also man update-alternatives. – jarno Mar 9 '20 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.