2

I am using the Bash shell on Ubuntu 18.04 LTS (Bionic Beaver). I would like to allow a user to define an environment variable in bash that can be used in subsequent terminal instances to set the verbosity of certain functions using that variable.

What would users have to do to enable and disable such capability? The focus is for the user to enable or disable the environment variable functionality without the need to change their .bashrc directly. The settings should only be applicable to a particular user wanting such capability.

6
  • Yes, of course. Please edit your question and tell us what shell you are using and what operating system. Also tell us if you want the variable to be set in all shell sessions for all users or only some users or some sessions.
    – terdon
    Commented Sep 19, 2022 at 18:00
  • What's the problem with changing .bashrc? Eventually you'll have to set the variable in some of the rc files.
    – aviro
    Commented Sep 19, 2022 at 18:29
  • Because I want users to have a quick way of interacting with the new tools. Without having to set things in the .bashrc. But perhaps they should if they want the capability enabled. But I have to be convinced.
    – Vera
    Commented Sep 19, 2022 at 19:18
  • 1
    Is this a duplicate of unix.stackexchange.com/questions/717775/… ?
    – Sotto Voce
    Commented Sep 19, 2022 at 20:44
  • The other just mentioned environment variables, but not how to use them for that special case. So this is not a duplicate.
    – Vera
    Commented Sep 19, 2022 at 20:48

2 Answers 2

1

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.

2
  • "To set a variable, all you need to do is set it." Are you sure, that this is what you meant to write? Commented Sep 19, 2022 at 21:19
  • 1
    @BrianReading yes, why? That's all that's needed. No need to edit any files, or anything like that. That's kinda the point of such variables: that you can just set them on the fly. This avoids needing to edit any files, unless you want the variable to be set for ever, and then every user can choose to do that themselves. You really don't want installers fiddling with user's settings if it can be avoided.
    – terdon
    Commented Sep 19, 2022 at 21:27
0

If I'm understanding your question correctly, you want a given user---if they desire it---to be able to activate the verbose option, e.g., mv -v---on certain commands. If this is correct, I have two suggestions:

  1. Teach your users how to use aliases. They can define the parameters of every command however they wish, including verbose output. If they want verbosity on six commands but not on the hundreds of others, they can have that. And if they want to run an aliased command without the alias for whatever reason, they can precede the command with a backslash.
  2. Create a bash_aliases file containing aliases you approve of and make it available to everyone on your network. A user source that file when they open a terminal, or add source <path>/bash_aliases somewhere in their ~/.bashrc.

I've no way of knowing how broad your bash knowledge is, so I'll offer my own bash_aliases file as an example. It serves me well.

You must log in to answer this question.

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