Details and examples of Linux environment variables

  • 2020-05-24 06:41:54
  • OfStack

Linux environment variable

~/.bash_profile && ~/.bashrc

Configuration files such as "/etc/profile", "~/.bash_profile "will be automatically executed when the user logs into the Linux operating system.

The execution process goes like this:

When logging into the Linux system, start "/etc/profile" first. Then launch "~/.bash_profile "in the user directory, If "~/.bash_login "and "~/.profile" exist, it will be executed.

Let's see what's in the file "~/.bash_profile"

$cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
# User specific environment and startup programs
alias py='/home/work/local/python/bin/python'

You can see that the ~/.bash_profile file first calls ~/.bashrc, and then loads PATH and LD_LIBRARY_PATH.

Environment variable related configuration files

/etc/profile: this file sets the environment information for each user of the system. When the user logs in for the first time, this file is executed and shell Settings are collected from the Settings file in the /etc/ profile.d directory. /etc/bashrc: execute this file for every user running bash shell. When bash shell is opened, the file is read. ~/.bash_profile: each user can use this file to enter shell information specifically for their own use, which is executed only once when the user logs in. By default, he sets some environment variables to execute the user's.bashrc file. ~/.bashrc: this file contains bash information dedicated to your bash shell, which is read when logging in and every time a new shell is opened. ~/.bash_logout: this file is executed each time you exit the system (exit bash shell).

/etc/profile is a global function in which the variables set are applied to all users, and the variables set in ~/.bash_profile can inherit the variables in /etc/profile and be used by users.

~/.bash_profile is interactive, login mode into bash operation ~/.bashrc is an interactive non-login way into the bash operation

Usually the two Settings are about the same, so usually the former will call the latter.

crontab execution environment

Everyone knows that crontab is a good thing. It can perform some tasks regularly, help you monitor the system status, and help you do some mechanical things repeatedly every day. One bad thing about crontab, however, is that it does not always read environment variable parameters from user profile files by default. This often results in a script that is successful when executed manually, but fails when crontab tries to get it to execute regularly

The problem with general crontab not running is caused by the fact that environment variables are not always recognizable in crontab.

When running crontab, the program is called in non_login mode, and ~/.bash_profile is not called in advance. Therefore, the crontab running environment is much smaller than the login running environment. If the program involves environment variables used by ~/.bash_profile, then parts of the program that work in login mode will not work under crontab.

Ultimate recommended solution:

30 12 * * * source ~/.bashrc && cd /home/work/mydir && ./myproj

Thank you for reading, I hope to help you, thank you for your support of this site!

Related articles: