Linux bash environment file

Setting environment variables in Linux using Bash

What is the equivalent to the tcsh setenv function in Bash? Is there a direct analog? The environment variables are for locating the executable.

5 Answers 5

export VAR=value will set VAR to value. Enclose it in single quotes if you want spaces, like export VAR=’my val’ . If you want the variable to be interpolated, use double quotes, like export VAR=»$MY_OTHER_VAR» .

PS: no need for the double quotes in the last one. The shell does not perform word splitting for variable assignments.

Just for the beginners. I had not understood it directly, here in normal English: interpolate «$X» means here that you already have a variable X that is filled with a value, e.g. «A», and you want to read out that value «A» first, and then assign this value as the value of VAR. You do not want VAR to be a string of «$X» here of course. And then the comment above seems also logical, that you will not even need the «». And for a direct assignment of «A», use ‘A’. Please correct me if I got it wrong.

The reason people often suggest writing

is that the longer form works in more different shells than the short form. If you know you’re dealing with bash , either works fine, of course.

Set a local and environment variable using Bash on Linux

Check for a local or environment variables for a variable called LOL in Bash:

el@server /home/el $ set | grep LOL el@server /home/el $ el@server /home/el $ env | grep LOL el@server /home/el $ 

Sanity check, no local or environment variable called LOL.

Set a local variable called LOL in local, but not environment. So set it:

el@server /home/el $ LOL="so wow much code" el@server /home/el $ set | grep LOL LOL='so wow much code' el@server /home/el $ env | grep LOL el@server /home/el $ 

Variable ‘LOL’ exists in local variables, but not environment variables. LOL will disappear if you restart the terminal, logout/login or run exec bash .

Читайте также:  Linux char to wchar

Set a local variable, and then clear out all local variables in Bash

el@server /home/el $ LOL="so wow much code" el@server /home/el $ set | grep LOL LOL='so wow much code' el@server /home/el $ exec bash el@server /home/el $ set | grep LOL el@server /home/el $ 

You could also just unset the one variable:

el@server /home/el $ LOL="so wow much code" el@server /home/el $ set | grep LOL LOL='so wow much code' el@server /home/el $ unset LOL el@server /home/el $ set | grep LOL el@server /home/el $ 

Local variable LOL is gone.

Promote a local variable to an environment variable:

el@server /home/el $ DOGE="such variable" el@server /home/el $ export DOGE el@server /home/el $ set | grep DOGE DOGE='such variable' el@server /home/el $ env | grep DOGE DOGE=such variable 

Note that exporting makes it show up as both a local variable and an environment variable.

Exported variable DOGE above survives a Bash reset:

el@server /home/el $ exec bash el@server /home/el $ env | grep DOGE DOGE=such variable el@server /home/el $ set | grep DOGE DOGE='such variable' 

Unset all environment variables:

You have to pull out a can of Chuck Norris to reset all environment variables without a logout/login:

el@server /home/el $ export CAN="chuck norris" el@server /home/el $ env | grep CAN CAN=chuck norris el@server /home/el $ set | grep CAN CAN='chuck norris' el@server /home/el $ env -i bash el@server /home/el $ set | grep CAN el@server /home/el $ env | grep CAN 

You created an environment variable, and then reset the terminal to get rid of them.

Or you could set and unset an environment variable manually like this:

el@server /home/el $ export FOO="bar" el@server /home/el $ env | grep FOO FOO=bar el@server /home/el $ unset FOO el@server /home/el $ env | grep FOO el@server /home/el $ 

Источник

How do I add environment variables?

I’m running Ubuntu 11.04. I use the terminal to start a bash session, and I want to add an environment variable:

(Anyway I’ll choose the most voted answer and leave the edited title -that wasn’t what I was asking)

env runs a program in a modified environment, then dismisses all the changes.

8 Answers 8

To set variable only for current shell:

To set it for current shell and all processes started from current shell:

export VARNAME="my value" # shorter, less portable version 

To set it permanently for all future bash sessions add such line to your .bashrc file in your $HOME directory.

Читайте также:  Linux show current directory

To set it permanently, and system wide (all users, all processes) add set variable in /etc/environment:

sudo -H gedit /etc/environment 

This file only accepts variable assignments like:

Do not use the export keyword here.

Use source ~/.bashrc in your terminal for the changes to take place immediately.

Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session. help.ubuntu.com/community/EnvironmentVariables

@BharadwajRaju because on some old UNIX systems one can export only variable that is set already. Old Solaris and HP-UX for example.

To set an environment variable once, use the export command in the prompt, not in a shell script:

The variable will be set for the rest of the shell session or until unset.

To set an environment variable everytime, use the export command in the .bashrc file (or the appropriate initialization file for your shell).

To set an environment variable from a script, use the export command in the script, and then source the script. If you execute the script it will not work.

For an explanation of the difference between sourcing and executing see this answer:

To permanently add a new environment variable in Ubuntu (tested only in 14.04), use the following steps:

  1. Open a terminal (by pressing Ctrl Alt T )
  2. sudo -H gedit /etc/environment
  3. Type your password
  4. Edit the text file just opened:
    e.g. if you want to add FOO=bar , then just write FOO=bar in a new line
  5. Save it
  6. Once saved, logout and login again.
  7. Your required changes are made.

I have cleaned up your answer removing the more dangerous bits about sudo gedit (See askubuntu.com/questions/270006/…) and chmod 777 . The latter should never be done on a system configuration file.

This is a correct answer for certain cases. All sessions, whether user shell sessions or not will get the value of variables set here. It is important to note that while this may look like a shell script, it is not — it allows you only to set environment variables (vs. using shell functions like if or test )

Читайте также:  Dr web workstation linux

@blue_chip for environment variables, everything is a string. You only need quotes when it contains spaces.

To get the environment/var changes to persist after the script has completed, you have to use source ./script.sh or the shorthand notation for source, «.», like . ./script.sh

Source will execute the commands in the script as if you have typed them in. so it does change some aspects of the script, such as exiting. so if your script checks something and decides to exit if false, for instance, via calling exit 0 , it will terminate your current terminal / shell session.

This is a very old question that already has an accepted answer and a few others. Consider, answering more recent questions please.

@NerdOfCode this answer addresses a problem that is still relevant and none of the other answers, including the accepted one, mention it, as far as I can see. There are in fact two badges available for providing good answers to old questions, so this is encouraged

This exactecly what was I looking for. I needed to see out of the script, the exported variables from the script.

@NerdOfCode how on the Earth can you discourage someone from providing additional information? This answer solved all my problems!

I know it’s pretty late, but if you want to add an environment variable for all users (e.g. JAVA usage) — you can do the following:

1) Open /etc/bash.bashrc using nano (you can use whatever editor, I do not prefer VIM as it’s the worst when it comes to user friendliness — nothing personal).

export VAR=path export PATH=$PATH:/bin 3) (Better if you can bounce the box) — or simply open a new SSH session and confirm using `env’ command.

BUT IF you want each user to have a separate setting for this, you have to make a change (scripted) to .bashrc file under ~/.bashrc (or /home/$USER/ if you are new to Linux systems)

Источник

Оцените статью
Adblock
detector