1

I often have to reconfigure my monitors for work, home, etc. on my laptop running 18.04. I wrote a simple shell aliased command in my .bashrc that configures them for the location (eg. wmc for "work monitor config").

Currently I open a terminal and execute the command, but it would be nicer to just hit a key (like the windows key) and type wmc without having to open the terminal and execute it.

How can I execute my aliased command without opening a terminal?

3
  • Doesn't creating a keyboard shortcut with the same command for wmc work?
    – pomsky
    Commented Apr 18, 2019 at 17:03
  • @pomsky Yeah. I was hoping executing the little command would be easier, but either way
    – user222963
    Commented Apr 18, 2019 at 17:18
  • ...or create desktop entries that show up when you hit the windows key and type wmc.
    – danzel
    Commented Apr 18, 2019 at 17:30

1 Answer 1

1

I found two ways that might work for you. Both involve using the shortcut ALT + F2, which opens a "Run Command" prompt.

  1. Enter your bash alias into the "Run Command" prompt, like so: bash -c <alias>.
  2. Export your alias code as a script, make it executable, put it into your PATH and run it with <alias> from the "Run Command" prompt.

Regarding solution 2, it is not really the alias that will be called but a script that has the same code as the alias. It could work like this:

Put a file with the name of your alias into the ~/.local/bin folder:

#!/bin/bash
<code of your alias>

and make it executable with chmod +x <file>

Hopefully, that helped

Update:

I realized that aliases are not expanded in non-interactive shell. While it is possible to use bash -ic <alias> (thanks to @steeldriver) to run an alias in interactive shell, this is NOT recommended, as the shell does not close after the command and stays open, which takes up processing power.

I would recommend solution 2 instead.

4
  • Does bash -c <alias> really work? I'd expect it to run a non-interactive shell (in which aliases are disabled by default). bash -ic <alias> might work though. Commented Apr 18, 2019 at 17:49
  • It seems you are right. bash -ic <alias> works, but then it stays open and takes up a lot of processing power (had to kill those processes manually)...So that does not seem like a good solution either...Any ideas?
    – icezyclon
    Commented Apr 18, 2019 at 18:24
  • 1
    Your script solution is how I'd do it (possibly with a keyboard shortcut to execute the script) - I don't see any value in trying to kludge it to work through an alias Commented Apr 18, 2019 at 18:27
  • I got the script to work from the command prompt (and could even assign a CTRL+ALT+SPACE hotkey to it), but only by writing the full path: sh "/home/me/local/bin/my-script". Neither the $PATH variable, nor an alias, nor ~ was understood. Commented Jun 28, 2019 at 9:22

You must log in to answer this question.