Linux print all environments

How to Print Environment Variables in Linux

Wondering what environment variables are set on your current shell? Learn how to print environment variables in Linux.

Environment variables are specific to certain environments. A generic answer, right?

But really, those are the variables that are just specific to your current system environment such as the currently logged-in user will be stored inside the «USER» variable.

Still confused? no worries. I will walk you through a brief understanding of environment variables and then jump to various ways to print them.

What are Environment Variables?

Nope, it’s not related to your Desktop Environment.

One of the most basic environment variables that you encounter on a daily basis is Hostname. So have you ever wondered why it’s always written in all caps?

Because most of the environment variables are pre-defined by the system and are global variables so you’ll encounter them often written in all caps.

So why use environment variables in the first place?

Suppose you are a programmer and your code needs to have access to your database key which should never be shared in public.

So what options do you have while sharing the entire code on Git? Wrap the database key to the environment variable.

This way you can set instructions on Git as «if you want to make this code work, interchange this variable with your database key.»

Of course, this was the one way of using environment variables. So let’s have a look at some common environment variables in Linux.

Environment Variable Description
HOME Shows home directory for the current user
HOSTNAME Contains Hostname of your system
UID Stores unique ID of the user
SHELL Shows the path to the shell which is being used currently
BASH_VERSION Contains version currently used bash instance
HISTFILE Path of the file where the history of commands is saved
TERM Shows the type of log-in terminal you’re using
PATH Shows path of commands and directories are separated by columns

There are various ways through which you can print environment variables in Linux and I’ll try to cover each of them. So let’s start with the easiest one.

Using printenev Command

The printenv utility is used to print environment variables for the current shell.

Suppose I want to print the value of USERNAME using printenv so my command will be as follows:

Use printenv to print environment variable

Similarly, you can also use the printenv utility to print more than one environment variable by separating them with .

So how about printing the value of HOME and USERNAME at once using printenv?

First, it’ll print the home directory of the currently logged-in user and in the second line, the hostname will be there.

Use printenv to print HOME and HOSTNAME

But what if you are looking for a way by which you can print each environment variable available in the current shell?

You can easily accomplish this by executing the printenv command without any options or arguments whatsoever.

Читайте также:  What is group permission in linux

Using echo Command

If you’ve been using Linux for a while, the chances of using this method to print environment variables are quite high (even if you were unaware that it’s not an environment variable).

Now, let me show you how you’re supposed to print the value of USERNAME with echo:

Print environment variable using echo command

But what if you want to print multiple environment variables using echo? just follow the given syntax:

So how about printing values of HOME, USERNAME, and HOSTNAME all at once using echo?

echo -e "$USERNAME \n$HOME \n$HOSTNAME"

Use echo command to print multiple values of environment variables

Using env Command

The env command is generally used by shell scripts to launch the correct interpreter but you can also use the env command to list available environment variables.

The env command if applied without any arguments and options, will print all the available environment variables.

Use env command to print environemnt variable

But what if you want to get the value of a specific environment variable? you can easily filter results with the grep command.

For demonstration, I’ll be printing out the value of HOME with the echo command:

Use env command to print environment variable

Using declare Command

The declare command is used to declare and print the value of variables in the shell.

Like any other example shown above, you can use declare without any argument and will bring the list of available variables.

Use declare command to print environment variables

Previously, I showed you how you can use the grep command to filter results but what if you want to filter more than one result?

First, let’s have a look at the syntax for filtering multiple environment variables:

For example, let’s print the value of HOSTNAME and USERNAME:

declare | grep 'HOSTNAME\|USERNAME'

Use grep with declare to print multiple environment variables

Using set Command

Generally, the set command is used to set and unset flags in the shell so that you determine the exact behavior for the upcoming process.

But you can also use the set command to print environment variables for the current shell.

To print each environment variable, use the given command:

Use set command to print environment variables

You can use the same approach used in the above method to filter results from the set command.

But still, let’s look at a simple example using the grep command to print multiple environment variables.

set | grep 'HISTFILESIZE\|HISTFILE\|GNOME_SHELL_SESSION_MODE'

Use set commad to print specific environment variables

Wrapping up

Though there are various methods for printing environment variables, I’d highly suggest you use the first method the syntax is the least complicated in terms of filtering specific variables.

And if you are still confused, the comments section is open to you.

Источник

Bash Print all Environment Variables

Environment variables are a collection of dynamic specified values kept inside the machine that has been utilized by programs running in terminals or subshells in Ubuntu and Linux platforms. The environment variable, in basic terms, is an attribute with a title and a value. Environment variables involve changing the way a system functions as well as the behavior of the device’s programs. The environment variable can hold data such as the regular word processor or search engine, the route to executable documents, the machine locale, and keypad layout preferences, among other things.

Set Environment Variable Value

You need to understand that the environment variables are initialized the same as the other variables in a programming language are initialized, i.e., bash variables. Although, the title of an environment variable is always case-sensitive, i.e., it must be uppercase. More than two values can be assigned to an environment variable using the colon “:”.

Читайте также:  Delete all file linux command

Here is a simple general syntax of initializing an environment variable. The set built-in command has been widely known to set the values of environment variables. If you use it without any argument or any set flag, it will make your shell display all the environment variables, as you can see from the image below. The common examples of “set” built-in are “set –x” to expand and display, “set –e” to quit the program upon encountering any non-zero status, “set –u” and “set –o” to display an error when it founds a variable with no set value, “set –n” to avoid executing the commands and many more.

Let’s start using different commands in the shell to print the environment variables. Before that, launch your console application using the “Ctrl+Alt+T” on the Ubuntu 20.04 desktop. The very first method to display all the environment variables is using the “env” command in the shell. But, it will only display the currently active environment variables of the system.

You can also pass some arguments in it to modify the output. We have used the simple “env” command to display all the current environment variables in our shell as per the output shown.

Let’s see another example to print all the environment variables in the shell. This time we have been using the “printenv” command in the shell to do so. The “printenv” command displays the currently active environment variables and the previously specified environment variables in the shell.

You can see the output of using the “printenv” command to display all the environment variables in the shell as per the snapshot below. We have got a lot of environment variables along with their assigned values, i.e., shell variables, display variables, authentication variables, and a lot more, as demonstrated from the image.

You can also use the same command with the keywords “more” and “less”. More commands will be helpful when you want to see more when needed. While the less command will automatically show fewer environment variables on your shell screen to avoid inconvenience. The commands for more and less display are stated as follows:

The more clear view for the output of the “more” command of printing environment variables is shown in the image below. Tap the “Enter” key to explore more variables.

The printenv command is very handy when it comes to transferring its variable data into other files. This means we can also transfer all the environment variables and their values to some bash file using the “printenv” command. You have to use the greater than sign after the “printenv” keyword along with the name of a file where the data of variables will be stored.

After doing this, you can see that the file will have all the environment variables. The output is the same for displaying the content of a file using the “cat” command and the “printenv” command in the shell.

On the other hand, you can also use the arguments within the “printenv” command to make it specific. Let’s say, if you want to check the values for the environment variable “HOME” in the shell, you have to mention it in the “printenv” command with the “grep” keyword. If the variable named “HOME” exists in the system, it will display it on the shell. As you can see, it displayed the “HOME” variable and its value, i.e., path in the shell.

Читайте также:  File system calls in linux

Let’s check for some other environment variables. Let say check for the folder “tmp” using the “grep” keyword. The folder “tmp” belongs to the variable “SESSION_MANAGER” in our system.

Now, let’s check about the “bin” folder that is widely used in the system. Upon running the command, we have got 4 environment variables in return, showing that it is a part of all those environment variables of the Ubuntu 20.04 system.

To check for the variable that doesn’t even exist in your system leads to an empty result. As the system has no environment variable for a folder or file “new”.

You can also use another command to display all the variables found in your system, i.e., not only the environment variables. The declare command can be used for this purpose along with the “-p” flag within the query.

If you only want to display the environment variables in your shell, you can also do that by declaring a command. You have to declare the “-xp” flag instead of the “-p” flag, as shown in the image. You can have a look at the output that displays only the environment variables.

Conclusion

This guide has been designed for all the bash users of the Linux system despite their learning capability. This is because all the examples implemented within this article are very simple and well-explained to make it understandable for every type of user.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.

Источник

How to list all Linux environment variables including LD_LIBRARY_PATH

How to list all the environment variables in Linux? When I type the command env or printenv it gives me lots of variables, but some variables like LD_LIBRARY_PATH and PKG_CONFIG don’t show up in this list. I want to type a command that list all the environment variables including this variables ( LD_LIBRARY_PATH and PKG_CONFIG )

3 Answers 3

env does list all environment variables.

If LD_LIBRARY_PATH is not there, then that variable was not declared; or was declared but not export ed, so that child processes do not inherit it.

If you are setting LD_LIBRARY_PATH in your shell start-up files, like .bash_profile or .bashrc make sure it is exported:

export LD_LIBRARY_PATH=/usr/local/lib:$

This will modify the variable.

To print it, type: echo $LD_LIBRARY_PATH and it should show the above value.

If you’re seeing nothing when you print that, then the variable might not be set.

The question in fact is a good question. when run env or printenv , the output will be the system environment, but LD_LIBRARY_PATH is not belong to.

For example, if you set a=1 , you can’t show it by env . Same as LD_LIBRARY_PATH, it is used by ld.so only(ld. so – this little program that starts all your applications)

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью
Adblock
detector