Hi,
I'm afraid there is no easy answer to your question. As you already read in the manpage the BASH_ENV variable simply holds the location of a script that will be sourced when bash is executed as a non-interactive shell. This is
one way to include some general functionality in all your scripts which are run in a non-interactive shell. So
you will have to decide if you need the variable. Here is a very simple example how to utilize BASH_ENV:
Create the following text file and save it in /etc/global
Code:
echo "'sayItAsItIs' function available!"
sayItAsItIs ()
{
echo "I'm just saying: $@"
}
Notice that there is
no shebang! If you set your BASH_ENV like
Code:
BASH_ENV=/etc/global
Then all your scripts will be able to call the function 'sayItAsItIs', e.g.
Code:
$ /bin/bash -c 'sayItAsItIs This script has no purpose!'
This will output
Code:
'sayItAsItIs' function available!
I'm just saying: This script has no purpose!
Real scripts will also be able to call 'sayItAsItIs':
Code:
$ cat script.bash
#!/bin/bash
sayItAsItIs No purpose
$ ./script.bash
'sayItAsItIs' function available!
I'm just saying: No purpose
$
So BASH_ENV holds the location of a "substitute" for .bashrc if the shell is non-interactive. The variable ENV comes into the picture when you call bash as 'sh'. Read the according section of the manpage again for more details.
I hope this very simple example makes it a bit clearer on how to use these variables. As for your questions:
Quote:
1. Where should I initialize $BASH_ENV and $ENV?
|
It depends. If you want all your users to have implicit functionality available in their scripts then declare them somewhere global, as in /etc/environment or /etc/bash.bashrc. Do not forget to make sure that the script that is to be sourced is also in a place which is accessible by every user.
If you want different implicit functionality for every user then declare them in their personal ~/.bashrc files. Put the script that is to be sourced in their home directories.
Quote:
2. Should I initialize these variables at all?
|
Same answer as for Q1. I personally have never used them. While this concept seems to implement some convenience there might be also side effects, e.g. what happens if you redefine a variable that has been previously sourced into your script? This is a potential source of trouble.
Quote:
3. If used, what is the default value of these variables?
|
I am not sure if there is any convention on how to name the script that is to be sourced. Since I haven't found anything in the manpage my guess is that there is no default value; I might be wrong though.
It is probably best to play with those two variables a bit to familiarize yourself with the concept. That's what LFS is there for. E.g., try the following variations of the above command:
Code:
$ /bin/bash -c -i 'sayItAsItIs This script has no purpose!'
# OR
$ /bin/sh -c 'sayItAsItIs This script has no purpose!'
Try to understand why those variations yield a different result.
Hope this helps.
PS:
Quote:
PS: Hope my English is understandable.
|
It is.