To set a variable, all you need to do is set it. For example, say you have a shell function that changes its behavior depending on whether a variable is set, like this:
check_verbosity(){
if [ -n "$VERBOSE" ]; then
echo "Am I verbose? Why yes, indeed I am, my friend!"
else
echo "no"
fi
}
You can then change its behavior by setting the variable accordingly. For example:
$ check_verbosity
no
$ VERBOSE=yes
$ check_verbosity
Am I verbose? Why yes, indeed I am, my friend!
You can even set the variable for that specific instance of the function only by defining the variable at the same time as launching the command:
$ VERBOSE=yes check_verbosity
Am I verbose? Why yes, indeed I am, my friend!
$ check_verbosity
no
So if you write functions and scripts that expect this variable, every user is free to set it as they want. If they want to make the change permanent, they can add it to their ~/.profile
or ~/.bash_profile
(if it exists) file to always have it set:
export VERBOSE=yes
This, the idea of programs reacting to variables being set or not in the environment they are run in, is a relatively common idiom. For example, ls
reacts to LS_COLORS
and grep
reacts to GREP_COLORS
(among others). You will see many man
pages have an ENVIRONMENT
section, usually near the end, that explains what environment variables the program has been written to react to.
.bashrc
? Eventually you'll have to set the variable in some of the rc files. – aviro Sep 19 '22 at 18:29.bashrc
. But perhaps they should if they want the capability enabled. But I have to be convinced. – Vera Sep 19 '22 at 19:18