Printing linux environment variables

How to print / echo environment variables?

The expansion $NAME to empty string is done by the shell earlier, before running echo , so at the time the NAME variable is passed to the echo command’s environment, the expansion is already done (to null string).

To get the same result in one command:

Note that only the printenv -based command preserves the semantics of the OP’s command: defining NAME as a command-scoped environment variable, which only the invoked command and its child processes see, but no subsequent shell commands. The other commands do something very different: they define NAME as an until-the-current-shell-exits shell-only variable, which all subsequent shell commands see, but no external utilities.

To bring the existing answers together with an important clarification:

As stated, the problem with NAME=sam echo «$NAME» is that $NAME gets expanded by the current shell before assignment NAME=sam takes effect.

Solutions that preserve the original semantics (of the (ineffective) solution attempt NAME=sam echo «$NAME» ):

Use either eval [1] (as in the question itself), or printenv (as added by Aaron McDaid to heemayl’s answer), or bash -c (from Ljm Dullaart’s answer), in descending order of efficiency:

NAME=sam eval 'echo "$NAME"' # use `eval` only if you fully control the command string NAME=sam printenv NAME NAME=sam bash -c 'echo "$NAME"' 

printenv is not a POSIX utility, but it is available on both Linux and macOS/BSD.

What this style of invocation ( = cmd . ) does is to define NAME :

  • as an environment variable
  • that is only defined for the command being invoked.

In other words: NAME only exists for the command (child process) being invoked, and has no effect on the current shell (if no variable named NAME existed before, there will be none after; a preexisting NAME variable remains unchanged).

POSIX defines the rules for this kind of invocation in its Command Search and Execution chapter.

The following solutions work very differently (quoted from heemayl’s answer):

NAME=sam; echo "$NAME" NAME=sam && echo "$NAME" 

While they produce the same output, they instead define:

  • a shell variable NAME (only) rather than an environment variable
    • if echo were a command that relied on environment variable NAME , it wouldn’t be defined (or potentially defined differently from earlier).

    Note that every environment variable is also exposed as a shell variable, but the inverse is not true: shell variables are only visible to the current shell and its subshells, but not to child processes, such as external utilities and (non-sourced) scripts (unless shell variables are designated as environment variables (too) with export or declare -x ).

    [1] Technically, bash is in violation of POSIX here (as is zsh ): Since eval is a special shell built-in, the preceding NAME=sam assignment should cause the the variable $NAME to remain in scope after the command finishes, but that’s not what happens.
    However, when you run bash in POSIX compatibility mode, it is compliant.
    dash and ksh are always compliant.
    The exact rules are complicated, and some aspects are left up to the implementations to decide; again, see Command Search and Execution.
    Also, the usual disclaimer applies: Use eval only on input you fully control or implicitly trust.

    Источник

    How to Set an Environment Variable in Linux

    Zaira Hira

    Zaira Hira

    How to Set an Environment Variable in Linux

    In programming, you use variables to store information like strings and numbers temporarily.

    Variables can be used repeatedly throughout the code or by your operating system to provide values. You can edit them, overwrite them, and delete them.

    In this tutorial, I’ll teach you what environment variables are and how to set them in Linux.

    What Are Environment Variables?

    Environment variables are the variables specific to a certain environment. For example, each user in an operating system has its own environment. An admin user has a different environment than other users do, for example.

    You might declare an environment variable only required by your user (for example a secret token) that doesn’t need to be exposed to other users.

    Here are some examples of environment variables in Linux:

    • USER – This points to the currently logged-in user.
    • HOME – This shows the home directory of the current user.
    • SHELL – This stores the path of the current user’s shell, such as bash or zsh.
    • LANG – This variable points to the current language/locales settings.
    • MAIL – This shows the location of where the current user’s mail is stored.

    These environment variables vary based on the current user session.

    How to List Environment Variables in Linux

    The command used to display all the environment variables defined for a current session is env .

    Here is the output for my session:

    image-194

    For the changes to take effect, update the .bashrc file using the source command:

    Let’s verify by opening a new session.

    image-196

    For the changes to take effect, use the command source /etc/environment .

    image-198

    • /etc/profile – Variables set in this file are read whenever a bash shell is logged in. Edit this file and use the export command:

    Now, I’ll switch the user to the root user and verify if I can access the variable GLOBAL_VARIABLE .

    root@Zaira:~# echo $GLOBAL_VARIABLE This is a global variable

    It worked! I have been able to access the global variable defined by the user Zaira through the root user as well. The same would apply to other users too. So now you also know how to define global environment variables.

    Conclusion

    In this tutorial, you learned how to create and define environment variables in Linux. You also learned how to make them persistent so that you can use them across multiple sessions.

    What’s your favorite thing you learned here? Let me know on Twitter!

    You can read my other posts here.

    Источник

    How to echo environment variable on Linux

    Environment variables contain data about the current system configuration. These variables are mostly referenced by scripts and system programs that need some information on the current configuration in order to adapt to various scenarios. For example, a script might check an environment variable to see what language is set on the computer, and then output prompts in the target language. One of the most commonly accessed environment variables is the PATH environment variable.

    Environment variables can be seen by any user on a Linux system by using the echo Linux command, among other methods. In this tutorial, you will learn how to echo an environment variable on a Linux system. This can be used to attain some configuration information or can be worked into a Bash script to make it respond differently depending on the results.

    In this tutorial you will learn:

    • How to echo environment variable on Linux

    How to echo environment variable on Linux

    Software Requirements and Linux Command Line Conventions
    Category Requirements, Conventions or Software Version Used
    System Any Linux distro
    Software N/A
    Other Privileged access to your Linux system as root or via the sudo command.
    Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
    $ – requires given linux commands to be executed as a regular non-privileged user

    How to echo environment variable

    We can echo an environment variable in Linux by using the echo command and the name of the variable we want to check. Here are some examples:

    $ echo $SHELL /bin/bash $ echo $HOME /home/linuxconfig $ echo $USER linuxconfig

    Let’s see how to make this a little more useful:

    $ echo "The current user is $USER, with the $SHELL shell, and has a home directory located at: $HOME" The current user is linuxconfig, with the /bin/bash shell, and has a home directory located at: /home/linuxconfig

    With the example above, it is easy to see how this functionality could be handy when writing a Bash script. It would allow a script to address the user by name, or create and manipulate directories or other system content by knowing where the home directory and other important things are located.

    Echo all variables

    Since it would not be reasonable to expect someone to know all the different pre-programmed variable names, you can get a full list of them by using the printenv command:

    Printing all the environment variables used by this Linux system

    DID YOU KNOW?
    A popular environment variable to edit is the $PATH variable, which lets you specify the directories Bash should search for programs when you enter a command. We’ve written a separate guide on how to add a directory to $PATH.

    Closing Thoughts

    In this tutorial, we saw how to echo environment variables on a Linux system. Environment variables are a useful convention in Linux shells that help facilitate system processes and user scripts. Without environment variables, we would not be able to obtain the type of information which can constantly change depending on different scenarios, such as which user is logged in, which desktop GUI is used, which directory a user is in, etc.

    Источник

    Читайте также:  Пароль кали линукс при входе
Оцените статью
Adblock
detector