0

I have exported a environment variable in my .zshrc and I want to update it from a script in /bin/script.sh

#!/bin/sh
if [ $TOGGLE -eq 1 ]; then
echo a;
else 
echo b;
fi

TOGGLE=$((TOGGLE * -1));

but after the run the script the value of TOGGLE is unchanged in the session where I run the script on. how can I force it to change

2
  • 2
    See mywiki.wooledge.org/BashFAQ/060 but zsh is not bash jfyi. Commented May 2, 2021 at 13:47
  • @RootOfMinusOne : You forgot to say how you run the script. If you execute it as source .../script.sh (but I hope you don't really have it stored in /bin, as your question implies), you should see the variable TOGGLE. Aside from this, it is not an environment variable, and you did not export something in any way. Commented May 4, 2021 at 7:50

1 Answer 1

2

This is a FAQ. Environment variables are private to each process, not global. Your script runs as a separate process and therefore cannot directly manipulate the env vars of its parent process. You could source the script which would cause it to be executed in the current shell process rather than a subshell. That, however, can have unwanted side effects to the state of the current shell.

Sign up to request clarification or add additional context in comments.

1 Comment

I created a function in my . zshrc and put the script in it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.