How to Set Environment Variables in Bash on Linux – Guide

This tutorial will show you how to set environment variables on Ubuntu, CentOS, Red Hat, basically any Linux distribution. You will also learn how to list all environment variables and how to replace (delete) existing environment variables. Environment variables are often used inside the bash shell. It is also a common means of configuring services and handling web application secrets. ..

It is common for environment-specific information, such as endpoints and passwords, to be stored as environment variables on a server. They are also used to define important directory locations for many popular packages, such as JAVA_HOME for Java.

Set environment variables in Bash

export MYVAR=‘myvar’

VAR = “abc”

“If you want to have spaces in your value, like ‘my value’ or ‘Linus Torvalds’, you will have to enclose your value in double quotes.”

export VAR=”my value”

Torvalds is the creator of the Linux kernel. ..

$HOME/.bash_profile: export HOME=/home/username

You can use the “printenv” command to print the value of your environment variable. ..

This time, you don’t need to precede it with a $ sign.

Setting variables using Bash interpolation

To set an environment variable for the output of a command on your server, you can use the following syntax: export [variable_name]=value ..

To achieve this, you will need to use Bash interpolation, also known as parameter substitution. ..

To store the value of your current shell instance in a variable called MYSHELL, you can use the following command: $MYSHELL ..

To set an environment variable using parameter substitution, use the export command and have the command enclosed in parentheses preceded by a dollar sign. ..

To set the environment variable “SHELL” in a new variable called “MYSHELL”, you would use the following command: ..

export MYSHELL=/bin/bash

You have successfully created your first environment variable in Bash! This is a great accomplishment, and it can be a valuable tool for managing your environment. ..

Setting permanent environment variables in Bash

export VAR=5 This command sets the value of VAR to 5. However, if you restart the shell, the new value will be applied.

export BASH_VERSION=4.3

This file contains the basic settings for the bash shell.

The following lines are examples. You can create your own by adding or deleting lines.

If you want to enable history substitution, add this line:

HISTSIZE=1000

If you want to disable command line editing, add this line:

COMMANDLINE_EDITING=false ..

export VAR=”My variable that I will always use”

If you are using a Bash shell, you will need to source your .bashrc file in order to make the changes take effect.

You can check the value of your environment variable in each shell by running: env -v

Each shell instance will create a new variable named “current_user” for the current user. ..

If you add the “–enable-ssh” option to your system’s “configure” script, ssh will be enabled by default.

The contents of the environment file are used to configure the GNU/Linux system. The file is located in the /etc directory.

This is a global variable. ..

After providing the file, environment variables will be set for each user on your host. The user’s operating system will be set, and any applications they may have installed will be run.

The global environment is a term used to describe the Earth’s atmosphere, hydrosphere, and lithosphere. ..

You’ve successfully set a global environment variable on your server that will allow you to access the internet from anywhere in the world.

Final note

This guide will show you how to set environment variables in Bash on Linux. If you have any query about this article, please ask us. Additionally, please share your love by sharing this article with your friends.