1

TLDR:

  • I have a simple shell script that flips an environment variable from value foo to bar, or the other way round, depending on its current value.
  • I use F12 to trigger this script, and I know that works.
  • When the script is triggered, the new value doesn't persist when the script ends.

What am I doing wrong?

Details:

1. The script:

My script checks whether the environment variable is present, or creates it if missing:
if [ -z $COLEMAK ];then export COLEMAK="qwerty";fi

Then the script flips the value from whatever it was to the other value:
if [ "$COLEMAK" == "qwerty" ] then export COLEMAK="colemak" ; xmodmap ~/colemak.map else export COLEMAK="qwerty" ; xmodmap ~/qwerty.map fi

Note that besides flipping the variable value, the script uses xmodmap to remap the keyboard from one layout to another. This is the core purpose of the script, and that's why I want to trigger it from a function key.

I know that this script only works when it is sourced (. ~/foo.sh), and that works well from the virtual terminal. So far so good.

2. xbindkeys:

I've configured .xbindkeysrc.scm to include (xbindkey '(F12) "term &") and that correctly executes the command when I press F12. So far so good.

I modified that line to read (xbindkey '(F12) ". ~/foo.sh &"). I know the script runs because it writes a log line when I press F12, but the change in the environment variable does not persist after the script ends.

1

That's the expected behavior. A program is only able to set environment variables for itself and its child processes.

In your case you could either store the current state in a file (like ~/.active-modemap) and act on the content of that file. Or you might be able to get the currently used modemap by looking (with grep etc) at the output of xmodmap -pke (or any other output of modemap).

1
  • Thank you - it makes sense of course that that's the expected behavior but I couldn't come up with an alternative. I'll use one of your suggestions instead! – Torben Gundtofte-Bruun Jan 12 '19 at 16:04

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.