The task is simple. I want to add a countdown timer to the prompt inside a bash script that is defined as an alias function in my .bashrc to make it run in the background when I need (eg "countdown 60 &"). So there is a "sleep 0.1" delay inside the loop that then updates PS1 until it reaches the final condition when the countdown ends and triggers a simple ringtone sound.

PS1 = PS1 + [countdown]

What it is happening to me is that the env variable gets updated but only when the script ends which don't fullfill the purpose of the script which is to show the state of the timer in the prompt. I can imagine that the script creates a copy of the calling context at runtime so that might be the issue.

How can I update an env variable to the global calling context on the fly?

EDIT: A different workaround I'm using. Writing to /dev/shm.

So what I did is writing to /dev/shm/<my_var> using it as a shared memory space (you could use /tmp as well) and read from there. I'm guessing ENV globals are available to read but scripts make a copy inside (I believe to prevent collisions) and then write back to the bind var once they finish the process.

If someone wants to know particulary how to dinamically change the prompt reading from the state of a custom variable, here is my script.

  export PSbase=$PS1;

  function countdown(){
     date1=$((`date +%s` + $1));
    while [ "$date1" -ge `date +%s` ]; do
      echo $(date -u --date @$(($date1 - `date +%s`)) +%H:%M:%S) > /dev/shm/countdown;
       sleep 0.1
     rm /dev/shm/countdown;
     paplay <path_to_sound>;
  function countdownToPS1 () {
    if [ -f /dev/shm/countdown ]; then
      export PS1="${PSbase%\$ } [$(cat /dev/shm/countdown)]$ ";
       export PS1=$PSbase;


And the usage is "countdown 50 &" and will show your prompt with a nice [HH:MM:SS] timer at the end. This evaluates the seconds so if I need 4 minutes it is "countdown 60*4 &". And then every time I press enter, PROMPT_COMMAND is instructed to execute the function that reads from /dev/shm/countdown and generates my new prompt with the current state of the timer. Nice. You can also wrap that function into timer() { countdown "$@" & } so you can star with type timer and the countdown will always run in the background.

So the question was how could I do that but working with just ENV variables and update them with a direct write on them not just a copy during the runtime of the script. I guess it is not possible.

1 Answer 1


That doesn't work at all: A script that you started from the current shell can only modify its own environment, not that of its parent shell.

  • Yeah, seems not. I though global variables were shared to every script at anytime but seems you can read from them but write when your script ends. not while running. I have solved writing to share memory (/dev/shm/countdown) and changing the PROMPT with a hook it seems to have with PROMPT_COMMAND. Thanks!
    – denik1981
    Commented Apr 28, 2021 at 20:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .