Change global environment variable using a script that is started using xbindkeys?












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.










share|improve this question





























    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.










    share|improve this question



























      1












      1








      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.










      share|improve this question
















      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.







      bash scripts environment-variables xbindkeys






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 8 at 22:14







      Torben Gundtofte-Bruun

















      asked Jan 8 at 22:04









      Torben Gundtofte-BruunTorben Gundtofte-Bruun

      4,4502463102




      4,4502463102






















          1 Answer
          1






          active

          oldest

          votes


















          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).






          share|improve this answer
























          • 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 at 16:04











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1108130%2fchange-global-environment-variable-using-a-script-that-is-started-using-xbindkey%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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).






          share|improve this answer
























          • 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 at 16:04
















          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).






          share|improve this answer
























          • 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 at 16:04














          1












          1








          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).






          share|improve this answer













          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).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 9 at 12:47









          RalfRalf

          20116




          20116













          • 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 at 16:04



















          • 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 at 16:04

















          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 at 16:04





          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 at 16:04


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Ask Ubuntu!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1108130%2fchange-global-environment-variable-using-a-script-that-is-started-using-xbindkey%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?