Linux user environment variables

How do I set environment variables?

I’m trying to set up Apache Tomcat on my pc, and it wants me to set up an environment variable for CATALINA_HOME . Does any know how to do this?

7 Answers 7

In bash you can set variables like this:

export CATALINA_HOME=/opt/catalina 

most other shells follow this convention, but not all. You can set it permanently in ~/.profile for bash (and as before, other shells have other locations)

for session-wide variables, help.ubuntu.com recommends ~/.profile as probably the best file for placing environment variable assignments in, since it gets executed automatically by the DisplayManager during the startup process desktop session as well as by the login shell when one logs-in from the textual console.

Updated the answer. The reason why i used .bashrc instead was that at some point .profile wasn’t sourced automatically. But if it works now, it’s better to use it.

It is to be noted that when you add new variables in ~/.bash_profile for instance, they won’t be automatically set in the environment until your next login. See man bash, and look for INVOCATION for more details.

To set permanent environment variables in latest Ubuntu versions (from 14.04 and above) add the variables to /etc/environment . For that follow the below instructions,

Open the terminal and run

sudo -H gedit /etc/environment 

the provide your password, then in the prompted text file

Читайте также:  Linux узнать свой ipv6

then add the variables like

Sample of the /etc/environment is given below

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" JAVA_HOME="/usr/lib/jvm/java-8-oracle/" AXIS2_HOME="/opt/axis2-1.7.4/" ANT_HOME="/opt/apache-ant-1.9.7/" 

don’t forget to logout and login again to enable the environment variables.

Environment variables should already work

If you are using the tomcat6 package from the Ubuntu repositories, then the CATALINA_HOME and other environment variables are already set, in the /etc/init.d/tomcat6 startup script.

If you are installing tomcat outside the package manager (hopefully in /opt or somewhere else outside the managed file system), then running the TOMCAT/bin/startup.sh should use the relative location to define the CATALINA_HOME.

Setting the Environment variable

If for some reason you still need to set an environment variable you can open a terminal window and type in the command:

export CATALINA_HOME=/path/to/the/root/folder/of/tomcat 

This environment variable will now work within that terminal window, but if you open another window or logout/login you loose that setting.

Make the environment variable permanent

To make the environment variable setting permanent, there are several places you can define the setting.

To be really sure the setting is being picked up, add the above setting to one of the startup script for tomcat:

yourtomcatfolder/bin/startup.sh yourtomcatfolder/bin/catalina.sh 

Note: startup.sh calls the catalina.sh. You should add the setting at the start of one of these files (after any initial comments)

The standard way for global environment variables would be to add an entry in /etc/environment (you do not use the command export in this file as it is not a normal bash script)

CATALINA_HOME=/path/to/the/root/folder/of/tomcat 

Not recommended

You can set the environment variables in the bash (command line shell) configuration files, but these are not recommended as they are not always picked up (eg. if you are running a server that you dont login to to run tomcat): ~/.bashrc | ~/.profile | /etc.bash.bashrc | /etc/profile

Читайте также:  Find file linux extension pack

Источник

How to Set an Environment Variable in Linux

Zaira Hira

Zaira Hira

How to Set an Environment Variable in Linux

In programming, you use variables to store information like strings and numbers temporarily.

Variables can be used repeatedly throughout the code or by your operating system to provide values. You can edit them, overwrite them, and delete them.

In this tutorial, I’ll teach you what environment variables are and how to set them in Linux.

What Are Environment Variables?

Environment variables are the variables specific to a certain environment. For example, each user in an operating system has its own environment. An admin user has a different environment than other users do, for example.

You might declare an environment variable only required by your user (for example a secret token) that doesn’t need to be exposed to other users.

Here are some examples of environment variables in Linux:

  • USER – This points to the currently logged-in user.
  • HOME – This shows the home directory of the current user.
  • SHELL – This stores the path of the current user’s shell, such as bash or zsh.
  • LANG – This variable points to the current language/locales settings.
  • MAIL – This shows the location of where the current user’s mail is stored.

These environment variables vary based on the current user session.

How to List Environment Variables in Linux

The command used to display all the environment variables defined for a current session is env .

Here is the output for my session:

image-194

For the changes to take effect, update the .bashrc file using the source command:

Let’s verify by opening a new session.

Читайте также:  Unable to execute linux

image-196

For the changes to take effect, use the command source /etc/environment .

image-198

  • /etc/profile – Variables set in this file are read whenever a bash shell is logged in. Edit this file and use the export command:

Now, I’ll switch the user to the root user and verify if I can access the variable GLOBAL_VARIABLE .

root@Zaira:~# echo $GLOBAL_VARIABLE This is a global variable

It worked! I have been able to access the global variable defined by the user Zaira through the root user as well. The same would apply to other users too. So now you also know how to define global environment variables.

Conclusion

In this tutorial, you learned how to create and define environment variables in Linux. You also learned how to make them persistent so that you can use them across multiple sessions.

What’s your favorite thing you learned here? Let me know on Twitter!

You can read my other posts here.

Источник

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