Understanding Linux Environment Variables: A Beginner's Guide

Sohail
Sohail

Table of Contents

Linux environment variables are named values with data that can include usernames, passwords, system paths, and other configurations that other software on the same operating system can use.

The purpose of env variables in Linux is to give software/tools a method to communicate without explicitly transferring information back and forth. So it may be simpler to configure and administer multiple tools on a system because they can all access the same data using the same environment variables.

Environment variables can also be used to indicate specific system parameters that must be used for a certain application or to alter the behaviour of an application.

Types of Linux Environment Variables

System variables, user variables, and process variables are only a few of the several kinds of environmental variables. All users on a system can access system variables since the operating system sets them.

Linux Environment variables

Users can only access user variables that they have created themselves. Only the currently operating process has access to process variables, which are set by the running process.

The scope of environment variables can also be used to categorise them; it may be system-wide or process-specific. Process-specific variables are only accessible by the process that sets them, whereas system-wide variables are accessible to all processes running on a system.

How to Create Proxy Server

Are you curious about how a proxy server works? Proxy servers useful to control and monitor internet traffic in your network. In this article, we will provide a step-by-step guide on how to create proxy server on Linux – Squid.

PATH Environment Variable

PATH is a system environment variable containing a list of directories separated by a delimiter character (such as a colon on Unix-based systems or a semicolon on Windows) that instructs the operating system where to search for executable files. When a user types a command into the terminal, the operating system searches the PATH variable for the associated executable file.

The PATH variable in Linux is critical in allowing users to run commands from anywhere in the system without having to specify the complete path to the executable file. This is due to the fact that Linux, like other Unix-based systems, uses a hierarchical file system, which arranges files and directories into a tree-like structure. Users would have to browse to the exact directory where the executable file is located without the PATH variable every time they wanted to perform a command.

In a Linux terminal, use the echo command followed by $PATH to see the current value of the PATH variable, as seen below:

echo $PATH

This will show the PATH variable’s current value which is a list of directories separated by colons.

To change the $PATH variable, we can edit the .bashrc file located in the home directory. The .bashrc file contains the settings for the shell. Editing the .bashrc file and adding or removing the necessary lines of code allows you to add or delete folders from the PATH variable. After making modifications, you must source the .bashrc file as follows for the changes to take effect:

source ~/.bashrc

You can also use the export command to change the PATH variable temporarily for the current terminal session, like this:

export PATH=/new/directory:$PATH

This will prepend the directory /new/directory to the current value of the PATH variable for the current terminal session only.

Setting Permanent Environment Variables in Linux

To set environment variables in Bash, use export command like so –

export VARIABLE_NAME=value

You can also set environment variables in Bash by adding them to .bashrc file resides in your home directory. Simply open the file in a text editor and insert the following line:

sudo nano /home/$USER/.bashrc
export VARIABLE_NAME=value

To set environment variables in Zsh, you can use the export command in the same way as in Bash:

export VARIABLE_NAME=value

You can also use the setenv command like this –

setenv VARIABLE_NAME value

You can also set environment variables in Zsh by adding them to the .zshrc file in your home directory.

The syntax and functionality of several commands varies significantly between Bash and Zsh environment variables. In Bash, for example, environment variables are normally set with the export command, whereas in Zsh, both the export and setenv procedures are available.

Zsh also supports more advanced functionality for interacting with environment variables, such as arrays and associative arrays.

To add permanent environment variables to /etc/environment, open the file in a text editor and add the following line –

VARIABLE_NAME=value

Each variable should be on its own line, and the file should not contain any export or setenv commands. After making changes to the /etc/environment file, you may need to log out and back in or restart your system for the changes to take effect.

Temporary Environment Variables

In a Linux terminal session you can also set temporary environment variables by using the export command –

export VARIABLE_NAME=value

Only the current terminal session will be affected by this environment variable setting. The variable will not be set if you start a new terminal session until you set it again.

The main difference between temporary and permanent environment variables is that the temporary environment variables are valid for the current terminal session, while permanent environment variables are stored in config file and remain valid after system reboots as well as terminal sessions. Permanent environment variables are normally set in the /etc/environment file for system-wide variables or in the .bashrc or .zshrc file in your home directory.

In Linux temporary environment variables have a variety of uses, such as –

Temporarily set an environment variable in the current terminal session to see how it affects your system in order to test a new environment variable without making any long-term modifications to your system.

Create temporary environment variables to configure your system to your preferences for that session if you wish to modify your environment for a particular activity or project.

Set temporary environment variables while working with sensitive data to prevent it from being saved permanently on your system. The variables will be removed along with any other data from that terminal session when you close it.

Use the export command to temporarily set environment variables inside of a shell script if you need to do so for a particular job. By doing this, it is made sure that the variables are only set during the script and have no bearing on the system as a whole.

Overall, using temporary environment variables is a good way to test and customise your Linux environment without permanently altering your system.

Google Drive on Linux: Tips and Tricks for Seamless Integration

Google Drive is the most popular cloud storage service, offering 15GB of free cloud storage. However, despite being such a popular cloud storage service, there is no official Google Drive client for Linux. There were even rumours that Google was planning to release an official Google Drive client for Linux, but nothing came to fruition.

Common Environment Variables in Linux

HOME - points to the home directory of the current user.
PATH - specifies the directories in which the shell should search for executable files.
LANG - specifies the default language and character encoding.
PWD - shows the present working directory.
SHELL - specifies the default shell for the current user.
USER - specifies the name of the current user.
DISPLAY - specifies the X Window System display to use.
LOGNAME - specifies the user name of the user who is currently logged in.
TERM - specifies the terminal type for the shell.
TZ - specifies the time zone.
LANGUAGE - specifies the default language.
SHLVL - specifies the current level of the shell.
LS_COLORS - specifies the color coding for the ls command.
PS1 - specifies the primary prompt string.
HOSTNAME - specifies the hostname of the system.
HISTSIZE - specifies the number of commands stored in the history list.
HISTFILESIZE - specifies the number of commands stored in the history file.
HISTFILE - specifies the history file location.
OLDPWD - specifies the previous working directory.
Linux TutorialsUncategorized

Comments