100% Pass Cisco, PMP, CISA, CISM, AWS Practice test on SALE! Get Now Get Now
Home/
Blog/
Mastering Linux: 6 Essential Environment Variable Hacks!
Mastering Linux: 6 Essential Environment Variable Hacks!
SPOTO 2024-01-15 16:43:31
Linux

Configuring Linux Environment Variables

During the customization of software installations, the frequent need arises to set up environment variables. The following enumerates diverse methods for configuring environment variables.

Example:

  • Operating System: Ubuntu 14.0
  • Username: spoto
  • Path for configuring MySQL environment variables: /home/spoto/mysql/bin

 

 

 

 

 

 

 

Accessing Environment Variables in Linux

To retrieve environment variables, employ the following methods:

  • Utilize the 'export' command to reveal all system-defined environment variables.
  • Employ the 'echo $PATH' command to display the current value of the PATH environment variable.

 

 

 

 

 

 

Executing these commands yields the following outcomes:

spoto:~export
declare -x HOME="/home/spoto"
declare -x LANG="en_US.UTF-8"
declare -x LANGUAGE="en_US:"
declare -x LESSCLOSE="/usr/bin/lesspipe %s %s"
declare -x LESSOPEN="| /usr/bin/lesspipe %s"
declare -x LOGNAME="spoto"
declare -x MAIL="/var/mail/spoto"
declare -x PATH="/home/spoto/bin:/home/spoto/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
declare -x SSH_TTY="/dev/pts/0"
declare -x TERM="xterm"
declare -x USER="spoto"

spoto:~ echo $PATH
/home/spoto/bin:/home/spoto/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

The PATH variable delineates the search paths for executing commands. It separates different paths with colons (:), and when defined using export, double quotes may or may not be included.

Linux Environment Variable Configuration Method 1: export PATH

Modify the value of PATH directly using the export command. Here is the method for configuring MySQL to enter the environment variable:

export PATH=/home/spoto/mysql/bin:PATH

# Or put the PATH in the front.
export PATH=PATH:/home/spoto/mysql/bin

Notes:

  • Activation Time: Immediate
  • Validity Period: Applicable for the current terminal session; becomes invalid after closing the window
  • Scope of Application: Effective exclusively for the current user
  • While configuring environment variables, it is crucial to include the original settings, specifically the $PATH segment, to prevent overwriting existing configurations.

 

 

 

 

 

 

Linux Environment Variable Configuration Method 2: vim ~/.bashrc

Configure by modifying the ~/.bashrc file located in the user's directory:

vim ~/.bashrc

# Add this to the last line
export PATH=$PATH:/home/spoto/mysql/bin

Notes:

  • Activation Time: Takes effect when a new terminal is opened using the same user or manually sourced using 'source ~/.bashrc.'
  • Duration of Effect: Permanently valid
  • Scope of Application: Applicable only to the current user
  • If subsequent environment variable loading files override the PATH definition, it may not take effect.

 

 

 

 

 

 

Linux Environment Variable Configuration Method 3: vim ~/.bash_profile

Similar to modifying the ~/.bashrc file, append the new path at the end of the file:

vim ~/.bash_profile

# Add this to the last line
export PATH=$PATH:/home/spoto/mysql/bin

Notes:

  • Activation Time: Takes effect when a new terminal is opened using the same user or manually sourced using 'source ~/.bash_profile.'
  • Duration of Effect: Permanently valid
  • Scope of Application: Applicable only to the current user
  • If there is no ~/.bash_profile file, you can edit the ~/.profile file or create a new one.

 

 

 

 

 

 

Linux Environment Variable Configuration Method 4: vim /etc/bashrc

This method involves modifying system configurations and requires administrator privileges (such as root) or write permissions for the file:

# If the /etc/bashrc file is not editable, it needs to be modified to be editable.
chmod -v u+w /etc/bashrc

vim /etc/bashrc

# Add this to the last line
export PATH=$PATH:/home/spoto/mysql/bin

Notes:

  • Activation Time: Takes effect upon opening a new terminal or manually sourcing /etc/bashrc.
  • Duration of Effect: Permanently valid
  • Scope of Application: Effective for all users

 

 

 

 

 

 

Linux Environment Variable Configuration Method 5: vim /etc/profile

This method involves modifying system configurations and requires administrator privileges or write permissions for the file, similar to vim /etc/bashrc:

# If the /etc/profile file is not editable, it needs to be modified to be editable.
chmod -v u+w /etc/profile

vim /etc/profile

# Add this to the last line
export PATH=$PATH:/home/spoto/mysql/bin

Notes:

  • Activation Time: Takes effect upon opening a new terminal or manually sourcing /etc/profile.
  • Duration of Effect: Permanently valid
  • Scope of Application: Effective for all users

 

 

 

 

 

 

Linux Environment Variable Configuration Method 6: vim /etc/environment

This method involves modifying the system's environment configuration file and requires administrator privileges or write permissions for the file:

# If the /etc/bashrc file is not editable, it needs to be modified to be editable.
chmod -v u+w /etc/environment

vim /etc/profile

# Add this to the last line
export PATH=$PATH:/home/spoto/mysql/bin

Notes:

  • Activation Time: Takes effect upon opening a new terminal or manually sourcing /etc/environment.
  • Duration of Effect: Permanently valid
  • Scope of Application: Effective for all users

 

 

 

 

 

 

Analysis of the Linux Environment Variable Loading Mechanism

The various configuration methods for environment variables have been outlined above. How does Linux load these configurations, and in what order?

Specific loading sequences can result in the overriding or ineffectiveness of environment variable definitions with identical names.

Classification of Environment Variables

Environment variables can be broadly categorized into user-defined environment variables and system-level environment variables.

  • User-level environment variable definition files: ~/.bashrc, ~/.profile (on some systems: ~/.bash_profile)
  • System-level environment variable definition files: /etc/bashrc, /etc/profile (on some systems: /etc/bash_profile), /etc/environment

 

 

 

 

 

 

Method for Testing the Loading Order of Linux Environment Variables

To test the loading order of environment variables in different files, we will define the environment variable UU_ORDER at the beginning of each environment variable definition file. The value of this variable will be the concatenation of its own value with the current file name. The files to be modified are as follows:

  • /etc/environment
  • /etc/profile
  • /etc/bash.bashrc
  • /etc/profile.d/test.sh
  • ~/.profile
  • ~/.bashrc

 

 

 

 

 

 

Add the following code as the first line in each file, and adjust the content after the colon to the absolute file name of the current file accordingly.

export UU_ORDER="$UU_ORDER:~/.bash_profile"

After making the modifications, save the changes. Open a new terminal window and observe the value of the variable by executing 'echo $UU_ORDER':

spoto:~echoUU_ORDER
$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc

The sequence for loading environment variables in Linux can be inferred as follows:

  1. /etc/environment
  2. /etc/profile
  3. /etc/bash.bashrc
  4. /etc/profile.d/test.sh
  5. ~/.profile
  6. ~/.bashrc

Detailed Explanation of Loading Linux Environment Variable Files

From the above test, it is evident that Linux loads environment variables in the following sequence:

  • System environment variables -> User-defined environment variables
  • /etc/environment -> /etc/profile -> ~/.profile

 

 

 

 

 

 

Upon opening the /etc/profile file, you will notice that it loads the /etc/bash.bashrc file. It then checks and loads .sh files in the /etc/profile.d/ directory.

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "PS1" ]; then
  if [ "BASH" ] && [ "BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1=' '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r i ]; then
      .i
    fi
  done
  unset i
fi

Subsequently, upon inspecting the ~/.profile file, it becomes apparent that this file includes the loading of the ~/.bashrc file.

# if running bash
if [ -n "BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "HOME/.bashrc" ]; then
    . "HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin directories
PATH="HOME/bin:HOME/.local/bin:PATH"

Tips

You can customize an environment variable file, such as defining 'spoto.profile' within a specific project. In this file, use 'export' to define a series of variables. Then, append 'source spoto.profile' at the end of the ~/.profile file. This allows you to access your personally defined set of variables in Shell scripts every time you log in.

Alternatively, you can use the 'alias' command to define aliases for specific commands. For instance, 'alias rm="rm -i"' (double quotes are necessary). Incorporating this code into ~/.profile ensures that every time you use the 'rm' command, it is essentially executing 'rm -i,' offering a convenient and efficient solution.

Latest Passing Reports from SPOTO Candidates
AI-900-P

AI-900-P

PCCSE-P

PCCSE-P

P2-7-FDN-P

P2-7-FDN-P

PMI-PMP-008

PMI-PMP-008

H12-821-E-P

H12-821-E-P

PCCSE-P

PCCSE-P

H19-338-E-P

H19-338-E-P

H19-315-E-P

H19-315-E-P

H12-891-E-P

H12-891-E-P

PCCSE-P

PCCSE-P

Write a Reply or Comment
Don't Risk Your Certification Exam Success – Take Real Exam Questions
Eligible to sit for Exam? 100% Exam Pass Guarantee
SPOTO Ebooks
Recent Posts
Excellent
4.9
Based on 2331 reviews
Request more information
I would like to receive email communications about product & offerings from SPOTO & its Affiliates.
I understand I can unsubscribe at any time.
Home/Blog/Mastering Linux: 6 Essential Environment Variable Hacks!
Mastering Linux: 6 Essential Environment Variable Hacks!
SPOTO 2024-01-15 16:43:31
Linux

Configuring Linux Environment Variables

During the customization of software installations, the frequent need arises to set up environment variables. The following enumerates diverse methods for configuring environment variables.

Example:

  • Operating System: Ubuntu 14.0
  • Username: spoto
  • Path for configuring MySQL environment variables: /home/spoto/mysql/bin

 

 

 

 

 

 

 

Accessing Environment Variables in Linux

To retrieve environment variables, employ the following methods:

  • Utilize the 'export' command to reveal all system-defined environment variables.
  • Employ the 'echo $PATH' command to display the current value of the PATH environment variable.

 

 

 

 

 

 

Executing these commands yields the following outcomes:

spoto:~export
declare -x HOME="/home/spoto"
declare -x LANG="en_US.UTF-8"
declare -x LANGUAGE="en_US:"
declare -x LESSCLOSE="/usr/bin/lesspipe %s %s"
declare -x LESSOPEN="| /usr/bin/lesspipe %s"
declare -x LOGNAME="spoto"
declare -x MAIL="/var/mail/spoto"
declare -x PATH="/home/spoto/bin:/home/spoto/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
declare -x SSH_TTY="/dev/pts/0"
declare -x TERM="xterm"
declare -x USER="spoto"

spoto:~ echo $PATH
/home/spoto/bin:/home/spoto/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

The PATH variable delineates the search paths for executing commands. It separates different paths with colons (:), and when defined using export, double quotes may or may not be included.

Linux Environment Variable Configuration Method 1: export PATH

Modify the value of PATH directly using the export command. Here is the method for configuring MySQL to enter the environment variable:

export PATH=/home/spoto/mysql/bin:PATH

# Or put the PATH in the front.
export PATH=PATH:/home/spoto/mysql/bin

Notes:

  • Activation Time: Immediate
  • Validity Period: Applicable for the current terminal session; becomes invalid after closing the window
  • Scope of Application: Effective exclusively for the current user
  • While configuring environment variables, it is crucial to include the original settings, specifically the $PATH segment, to prevent overwriting existing configurations.

 

 

 

 

 

 

Linux Environment Variable Configuration Method 2: vim ~/.bashrc

Configure by modifying the ~/.bashrc file located in the user's directory:

vim ~/.bashrc

# Add this to the last line
export PATH=$PATH:/home/spoto/mysql/bin

Notes:

  • Activation Time: Takes effect when a new terminal is opened using the same user or manually sourced using 'source ~/.bashrc.'
  • Duration of Effect: Permanently valid
  • Scope of Application: Applicable only to the current user
  • If subsequent environment variable loading files override the PATH definition, it may not take effect.

 

 

 

 

 

 

Linux Environment Variable Configuration Method 3: vim ~/.bash_profile

Similar to modifying the ~/.bashrc file, append the new path at the end of the file:

vim ~/.bash_profile

# Add this to the last line
export PATH=$PATH:/home/spoto/mysql/bin

Notes:

  • Activation Time: Takes effect when a new terminal is opened using the same user or manually sourced using 'source ~/.bash_profile.'
  • Duration of Effect: Permanently valid
  • Scope of Application: Applicable only to the current user
  • If there is no ~/.bash_profile file, you can edit the ~/.profile file or create a new one.

 

 

 

 

 

 

Linux Environment Variable Configuration Method 4: vim /etc/bashrc

This method involves modifying system configurations and requires administrator privileges (such as root) or write permissions for the file:

# If the /etc/bashrc file is not editable, it needs to be modified to be editable.
chmod -v u+w /etc/bashrc

vim /etc/bashrc

# Add this to the last line
export PATH=$PATH:/home/spoto/mysql/bin

Notes:

  • Activation Time: Takes effect upon opening a new terminal or manually sourcing /etc/bashrc.
  • Duration of Effect: Permanently valid
  • Scope of Application: Effective for all users

 

 

 

 

 

 

Linux Environment Variable Configuration Method 5: vim /etc/profile

This method involves modifying system configurations and requires administrator privileges or write permissions for the file, similar to vim /etc/bashrc:

# If the /etc/profile file is not editable, it needs to be modified to be editable.
chmod -v u+w /etc/profile

vim /etc/profile

# Add this to the last line
export PATH=$PATH:/home/spoto/mysql/bin

Notes:

  • Activation Time: Takes effect upon opening a new terminal or manually sourcing /etc/profile.
  • Duration of Effect: Permanently valid
  • Scope of Application: Effective for all users

 

 

 

 

 

 

Linux Environment Variable Configuration Method 6: vim /etc/environment

This method involves modifying the system's environment configuration file and requires administrator privileges or write permissions for the file:

# If the /etc/bashrc file is not editable, it needs to be modified to be editable.
chmod -v u+w /etc/environment

vim /etc/profile

# Add this to the last line
export PATH=$PATH:/home/spoto/mysql/bin

Notes:

  • Activation Time: Takes effect upon opening a new terminal or manually sourcing /etc/environment.
  • Duration of Effect: Permanently valid
  • Scope of Application: Effective for all users

 

 

 

 

 

 

Analysis of the Linux Environment Variable Loading Mechanism

The various configuration methods for environment variables have been outlined above. How does Linux load these configurations, and in what order?

Specific loading sequences can result in the overriding or ineffectiveness of environment variable definitions with identical names.

Classification of Environment Variables

Environment variables can be broadly categorized into user-defined environment variables and system-level environment variables.

  • User-level environment variable definition files: ~/.bashrc, ~/.profile (on some systems: ~/.bash_profile)
  • System-level environment variable definition files: /etc/bashrc, /etc/profile (on some systems: /etc/bash_profile), /etc/environment

 

 

 

 

 

 

Method for Testing the Loading Order of Linux Environment Variables

To test the loading order of environment variables in different files, we will define the environment variable UU_ORDER at the beginning of each environment variable definition file. The value of this variable will be the concatenation of its own value with the current file name. The files to be modified are as follows:

  • /etc/environment
  • /etc/profile
  • /etc/bash.bashrc
  • /etc/profile.d/test.sh
  • ~/.profile
  • ~/.bashrc

 

 

 

 

 

 

Add the following code as the first line in each file, and adjust the content after the colon to the absolute file name of the current file accordingly.

export UU_ORDER="$UU_ORDER:~/.bash_profile"

After making the modifications, save the changes. Open a new terminal window and observe the value of the variable by executing 'echo $UU_ORDER':

spoto:~echoUU_ORDER
$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc

The sequence for loading environment variables in Linux can be inferred as follows:

  1. /etc/environment
  2. /etc/profile
  3. /etc/bash.bashrc
  4. /etc/profile.d/test.sh
  5. ~/.profile
  6. ~/.bashrc

Detailed Explanation of Loading Linux Environment Variable Files

From the above test, it is evident that Linux loads environment variables in the following sequence:

  • System environment variables -> User-defined environment variables
  • /etc/environment -> /etc/profile -> ~/.profile

 

 

 

 

 

 

Upon opening the /etc/profile file, you will notice that it loads the /etc/bash.bashrc file. It then checks and loads .sh files in the /etc/profile.d/ directory.

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "PS1" ]; then
  if [ "BASH" ] && [ "BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1=' '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r i ]; then
      .i
    fi
  done
  unset i
fi

Subsequently, upon inspecting the ~/.profile file, it becomes apparent that this file includes the loading of the ~/.bashrc file.

# if running bash
if [ -n "BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "HOME/.bashrc" ]; then
    . "HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin directories
PATH="HOME/bin:HOME/.local/bin:PATH"

Tips

You can customize an environment variable file, such as defining 'spoto.profile' within a specific project. In this file, use 'export' to define a series of variables. Then, append 'source spoto.profile' at the end of the ~/.profile file. This allows you to access your personally defined set of variables in Shell scripts every time you log in.

Alternatively, you can use the 'alias' command to define aliases for specific commands. For instance, 'alias rm="rm -i"' (double quotes are necessary). Incorporating this code into ~/.profile ensures that every time you use the 'rm' command, it is essentially executing 'rm -i,' offering a convenient and efficient solution.

Latest Passing Reports from SPOTO Candidates
AI-900-P
PCCSE-P
P2-7-FDN-P
PMI-PMP-008
H12-821-E-P
PCCSE-P
H19-338-E-P
H19-315-E-P
H12-891-E-P
PCCSE-P
Write a Reply or Comment
Don't Risk Your Certification Exam Success – Take Real Exam Questions
Eligible to sit for Exam? 100% Exam Pass GuaranteeEligible to sit for Exam? 100% Exam Pass Guarantee
SPOTO Ebooks
Recent Posts
2024 PMP Exam: 5 Key Preparation Tips
2024 Huawei Datacom Certification Roadmap
2024 Huawei HCIE Lab Exam Guide
CPIM or CSCP? How to Choose?
CPIM Exam Prep Guide in 2024
What is CPIM Certification?
Mastering QoS for Cisco CCDE
2024 Comprehensive Guide: Master the Azure Key Vault
Understanding MPLS Traffic Engineering: Key Concepts and Terminologies
Understanding Multicast Routing in Cisco Networks
Excellent
4.9
Based on 638 reviews
Request more information
I would like to receive email communications about product & offerings from SPOTO & its Affiliates.
I understand I can unsubscribe at any time.