I have a script that increments an environment variable, but it only works if I run it by perpending it with "source" or with a ".". Unfortunately, this means that if I attempt to bind it to a key, I can't seem to use "source" modifier, thus the environment variables don't actually change.

(Assuming an integer TEST already exists within shell environment):


let "TEST=$TEST+1"
echo $TEST
export TEST=$TEST

Ideally this script would increment the test variable, but (if I understand this right) it only does so in the "sub-shell" environment that this scripts spawns when it is run. Is it possible modify an environment variable in this way without using "source"? If not, how else could I accomplish my goal? I'd appreciate any help, thanks!

  • Which way would you bind it to a key? Inside bash/readline? And how would you use it? – Volker Siegel Aug 10 '14 at 6:15
  • The way I've been binding keys thus far is through the Keyboard part of the settings window (under the "Application Shortcuts" tab of the Keyboard window). A key I could use while operating in a GUI. I'm trying to write a toggle for disabling my track pad. But it seems I can't query whether it's disabled or not, only disable and enable it so I decided to keep track of this via a global variable. But even if there's an alternate solution to that, I would still like to know if there's a way to accomplish this task, or a workaround. – user2840488 Aug 10 '14 at 6:24
  • For the track pad, it should be possible to check the disabled state with something like xinput list-props 13 | grep 'Device Enabled.*1' for device 13 - see help.ubuntu.com/community/SynapticsTouchpad – Volker Siegel Aug 10 '14 at 6:42
  • Gah! I was looking for that for literally an hour! I'm used to scouring docs but the man pages just felt ridiculous, I totally missed that this info was even present. I was thinking it seemed weird that it wasn't... Thanks again! – user2840488 Aug 10 '14 at 7:04

The easiest solution is probably to store the variable in a file.

Write to a file:

echo "13" > /path/to/my-variable.txt

Read the first line in a file into the variable line:

read -r line < /path/to/my-variable.txt

|improve this answer|||||
  • Great idea! I finished the script using this. If possible, I'd like to do this without an extraneous file because that will grow bulky and difficult to maneuver with time, but for the foreseeable future this will work just fine, thanks! – user2840488 Aug 10 '14 at 6:25
  • @user2840488 I think you will need the file - where else do you want to save the variable value? There are other ways, but they are more complicated. – Volker Siegel Aug 10 '14 at 6:45
  • Could you point me in the right direction? I totally understand if it's over my head, I'm a java guy so most of this syntax is just stopping me dead in my tracks, but I'd like something in the back of my mind I could take another crack at in a few months when I'm not so green. Thanks for the help again! – user2840488 Aug 10 '14 at 7:00

Similar to the answer provided about using a .txt file, you could probably do something with a simple key-value store like redis or memcached (or a db, but that's really overkill).

$ sudo apt-get install redis-server redis-tools

$ redis-cli SET testing 5

$ redis-cli GET testing 
|improve this answer|||||

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.