Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Question 1:
if echo :"$PATH": | grep :"$SomeDirectory": > /dev/null
then echo "$SomeDirectory" is in the current PATH
else echo "$SomeDirectory" is not in the current PATH
fi
Question 2:
How do you know it isn't working?
From your results, we can't tell whether or not $SOMETHING
is empty. For example it may have a space character.
It looks to me like it is working. The logic says if
$SOMETHING is empty then echo yes, and it did.
AFAIK there is nothing wrong. The first time SOMETHING is empty (and exporting SOMETHING would then affect the environment of the current shell and children) but since you dumped the export in your .bashrc instead (which isn't exactly behaviour you would want to propagate beyond your own system) it gets sourced when starting a new shell session. See the "invocation" part in 'man bash'?
variable is added to the .bashrc file and *seems* that the first command (exporting to current shell) was not added because echo command return nothing.
Please re-read the post from unSpawn, since it has the answer to your question:
Code:
.bashrc ... gets sourced when starting a new shell session
this means that it's not enough to write the export statement into $HOME/.bashrc... it has to be sourced to apply the changes (otherwise it is simply something written in a text file and nothing more). If you want to apply the changes to the current shell just do:
Code:
. $HOME/.bashrc
Anyway, what is the aim of this? If you want to provide an installation script, I think it's better to not modify the user's .bashrc without he/she is aware of the change. I personally would prefer the installation script warns me about the changes I must apply (that is it makes notice about a necessary environment variable) then I will apply it at my pleasure, eventually following the advice to write the export statement in my .bashrc. Just my
Finally, following again the answer from unSpawn, you should definitively have a look at the bash man page or at the Bash reference manual to see how environment variables work and how they are assigned by the login shell when invoked.
believe me that sourcing .bashrc in a bash file does not work.
Believe me, I believe you! The shell cannot retrieve environment variable from its childs. Every variable assigned into the script (even with a source command) is lost when the script terminates. The parent shell will not be aware of the changes.
Indeed it would not be useful at all trying to set environment variables from a script. This is the reason why configuration files like .bashrc, .bash_profile or the system-wide /etc/bashrc and so on, exist and are sourced every time you invoke a new shell.
In your example, you modify the $HOME/.bashrc from a script, but you have to source it in the parent shell (after the script has finished its job) to see the changes applied to the current shell (without closing it).
So I think I have to leave it at this time until finding a better solution. I will write in my readme file that the users should enter the variables manually and then run the install script.
Using this solution will then satisfy the privacy of users because they are aware of changing their env variables.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.