The printenv is a command that shows the environment variable value. If no VARIABLE is specified, the display name of the environment variable along with its value in pairs for all variables will be displayed i.e. printenv is used to display the values of all environment variables.
The printenv HOME command displays the current user’s home directory location.
Another technique to show the value of the HOME environment variable is to use an echo with the $ (dollar) symbol in front of it. On the screen underneath, you can verify an illustration of it.
The env command is another related command that may be used to print the value of the environment variables. When used with the “$HOME” argument, it returns the error “Permission denied” because the path to a variable is protected with sudo rights.
With no arguments in the “env” command, it is functionally equal to the printenv command.
Example 1: Declare an Environment Variable
To declare a new environment variable for Git, we need to follow along with the commands in the shell. So, open a Bash shell command-line (terminal). Using this easy syntax, create and specify a new environment variable that is available to your running command-line shell and any applications started from it. The command below creates a new environment variable named VARIABLE NAME with the value “[VALUE]” in it. We can incorporate space in the string while still interpreting it as a single value by using the double quotes around “[VALUE]”. To check the value of an environment variable, just utilize the echo command and link to the variable listed below. This will show the present value of the variable VARIABLE_NAME, which is [VALUE] at this time.
When you refer to an environment variable in a command, make sure you put a $ in front of it so the shell knows you’re talking about an environment variable rather than a file or another application program. You can call your variables whatever you wish, however for environment variables, we commonly use all uppercase text. This separates them from the variety of command-line commands, apps, and files, which are frequently written in lower case.
$ echo $VARIABLE_NAME
Example 2: Predefined Environment Variables
Environment variables can be used in any command, and most systems already have a few set up for you. The title of the presently logged-in user is normally set in the environment variable $USER. You can use the echo statement to print and check the value of this, but now this time, we can also add a welcoming message. Take note of the double quote marks. These are used to surround a string that may contain spaces or characters that the shell interprets differently.
However, environment variables can still be used within these strings. Before the string is provided to the echo command, the variables will be extended to their current value. When you use the double quotations, the $USER is expanded; when you use the single quotes, the $USER is viewed as literal text and not a variable to be expanded.
Example 3: Export and Unset an Environment Variable
In this example, we first create the DIR environment variable within the Git and give it the value /home/Linux/. We utilized the echo instruction to print the value saved in DIR for verification.
$ echo $DIR
In Linux, you can set or unset user-specific or session environment variables. By default, the “env” instruction provides a list of all current environment variables. However, when used with the ‘-i’ flag, it temporarily clears off all the environment variables and allows the user to run a command in the current session without them. The unset command can be used to clear local environment variables temporarily.
$ echo $DIR
We have created an environment variable “DIR” for our Git repository “project1”.
$ echo $DIR
The command will provide you with a Bash shell that doesn’t have any environment variables for the time being. However, when you exit the shell, all variables will be restored.
env –I bash
In Linux, you can set and unset user-specific environment variables. You must add and edit the “.bashrc” file in the home directory to export or change the environment variable. Then, to make the changes take effect, source the file.
The variable (in our case, ‘CD’) would then become active. When the user launches a new terminal, this variable will be exposed. Add the appended line at the end of the file and save it.
Apply the source command and then print the value of CD using echo command.
$ echo $CD
This is all about setting environment variables in a shell terminal of a system with Git installed and configured. To do that, we have tried a bundle of some commands like env, printenv, unset, export, and echo. We hope you will implement this article on your operating system. Check more articles in the Linux Hint website.