Linux environment variables home directory

Setting and Using Linux Environment Variables

Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.

Your Linux shell has access to an environment that stores configuration values and other information in environment variables. Accessing these variables can be useful when working with shell commands. You can also set environment variables that can be accessed and used by your scripts, and applications. This guide walks you through the fundamentals of accessing, creating, and using environment variables.

Before You Begin

  1. If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.
  2. Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.

This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo . If you’re not familiar with the sudo command, see the Users and Groups guide.

What Are Environment Variables?

Environment variables are name-value pairs used by a system’s shell to define default shell properties. Some of these include your shell’s home directory, prompt, and current working directory. Environment variables are inherited by sub-shells, and are available to applications, and daemons. You can also create and set your own environment variables.

Environment Variable Scope

A variable’s scope refers to the parts of a program or environment that can access a given variable. An environment variable in Linux can have global or local scope.

  • Globally scoped environment variables are accessible from anywhere in a particular environment bound by the terminal.
  • Locally scoped environment variables can only be accessed by the terminal that defined the variable. It cannot be accessed by any program or script.

Differences Between Environment and Shell Variables

Standard UNIX variables are classified into two categories—environment variables and shell variables.

Environment variables:

  • An environment variable is available and valid system-wide.
  • Environment variables can be used by scripts and applications.
  • These variables are inherited by all spawned child processes and shells.
  • By convention, environment variables are given upper case names.

Shell variables:

  • Shell variables are available only in the current shell session.
  • These variables are useful when you need to store values temporarily.
  • Each shell such as zsh and bash , has its own set of internal shell variables.
Читайте также:  Re4son raspberry kali linux

This guide focuses on environment variables with references to shell variables.

Commonly Used (Global) Environment Variables

The following environment variables are commonly available to most popular Linux system’s by default.

  • USER : The currently logged-in user name.
  • HOME : The path to the current user’s home directory.
  • SHELL : The pathname of the current user’s shell.
  • PATH : A list of directories that the shell searches for executable files.
  • PWD : The path to your current working directory (PWD stands for “Print Working Directory”).
  • UID : The current user’s unique identifier.

Follow the steps below to view your system’s values for the environment variables listed above.

    Create a new file named variables.sh and include the contents of the example file.

The current logged-in User is: example_user Home directory of the current user is: /home/example_user Pathname of the current user's shell is: /bin/bash The Present Working Directory is: /home/example_user Users unique identifier is: 1000

How to Get and Set Environment Variables

List All Environment Variables

To view a list of all (global) environment variables available to the current user, use the following command :

You can pipe the output of the printenv command to the less utility to page through all your environment variables.

To get a more comprehensive list of (global and local) environment variables, use the set command. This list includes environment variables, shell variables, and functions.

Get the Value of an Environment Variable

To see the value of a single environment variable, use the following command and replace VARIABLE_NAME with your own variable.

For example, you can pass the HOME variable as an argument to the command.

Alternatively, you can use the echo command, and prepend the variable’s name with the $ symbol to output the value of the variable. This works for both environment variables and shell variables.

How to Set Environment Variables

To set the value of an existing environment variable type the variable name followed by the value you want to assign.

EXAMPLE_VARIABLE='example value' 

If the environment variable does not already exist, it is created as a shell variable. You can promote the shell variable to an environment variable by exporting it. See the How to Export Environment Variables section for details.

Use the set command to view the variable and its value.

set | grep EXAMPLE_VARIABLE 
EXAMPLE_VARIABLE='example value'

How to Export Environment Variables

You can turn a shell variable into an environment variable using the export command. The example below creates a variable called EXAMPLE_VARIABLE and assigns it the value new example value .

export EXAMPLE_VARIABLE='new example value' 

Use the printenv to confirm that the variable is now a part of your environment.

How to Assign Multiple Values to an Environment Variable

To assign multiple values to an environment variable use a list. Each value should be separated by a colon.

export EXAMPLE_VARIABLE=/path/to/first/location:/path/to/second/location 

You can also append new items to lists. The example appends the example-directory located in the user’s home directory to the PATH environment variable.

export PATH=$PATH:$HOME/example-directory 

PATH is a default environment variable that defines directories where your shell can look for executables. This variable allows you to run an executable without having to specify its path.

Читайте также:  Linux find exec отсутствует аргумент

How to Unset an Environment Variable

The unset command removes an environment or shell variable from the session. This example removes EXAMPLE_VARIABLE .

Persisting Environment Variables

You can set an environment variable permanently between shell sessions and users. To do so, set your environment variable in your shell configuration file using the export command

The example in this section uses the Bash shell, since it is the default shell on most popular Linux distributions. If you are using any other shell, modify the steps accordingly.

Use the SHELL environment variable to check which shell you are using:

Set a persistent environment variable for a single user:

Edit the user’s .bashrc file located in their home directory and add a line that exports the environment variable that you want to persist. The example line appends the user’s /home/username/example-directory to the PATH variable.

Set a persistent environment variable for all system users:

Create a new shell script file ( .sh ) in the /etc/profile.d directory. Add a line to your example file that exports the environment variable that you want to persist across all system users.

You can also add environment variables to the /etc/profile or the /etc/bashrc files. However, your variables may not persist after upgrades to your shell package.

Using Environment Variables in Application Development

In application development, you can use environment variables to distinguish between application environments and to store configuration information required by your app. The example below uses environment variables to distinguish between test and production application environments and to store URLs for each environment’s corresponding API.

Create a .sh file to store your app’s configuration values as environment variables. You can store the file in the /etc/profile.d/ directory to make the configurations available to all system users. The APP_ENV variable can then act as a switch in your application code.

Ensure you reload your system’s profile file to give your terminal session access to your new environment variables:

Create a new file named main.py with the example content below. The example application reads the APP_ENV variable and fetches the configurations from the appropriate variables. This method makes the application environment agnostic — the application responds appropriately depending on its current environment.

Execute the file to view which environment and API URL is currently detected by the example application.

Your current environment: TEST Your APIs URL: https://api.test.example.com/v1/customers

Depending on your system’s configuration and installed version of Python, you may need to adjust the above command to explicitly use Python 3.

When you need to change to the production environment, update the APP_ENV variable to PROD .

Rerun the main,py file and your output should now indicate that you are using the configurations for your production environment.

Your current environment: PROD Your APIs URL: https://api.example.com/v1/customers

Conclusion

The information covered in this guide should help you start using environment variables on your Linux server. They can be strong tools for scripting, developing applications, and having a better time working in the shell overall.

Читайте также:  Установить линукс минт через терминал

This page was originally published on Tuesday, May 4, 2021.

Источник

Where is the $HOME environment variable set?

I’m looking for the place where $HOME environment variable set. It is after login, to my mind. I’m using Linux debian 2.6.32-5-686.

5 Answers 5

(it is defined in /etc/passwd ; edit it with usermod -d /some/new/home/dir myusername , though modifying the file may work)

(the string is passed in as an argument to the login program, which will later set the $HOME env var)

sidenote: When the $HOME variable is not defined, the bash shell will fetch it from its official definition in /etc/passwd and define it in that script. This may indicate that your scripting or programming language may have special treatment for the $HOME variable since it is slightly ‘important’ (though perhaps not as much as $PATH).

You are likely confusing $HOME with $PATH . It does not make sense to have multiple paths in $HOME (the entire value will be treated as a single pathname) or, in most cases, to modify $HOME at all.

On Linux, the HOME environment variable is set by the login program:

  • by login on console, telnet and rlogin sessions
  • by sshd for SSH connections
  • by gdm , kdm or xdm for graphical sessions.

The login program arranges it before calling exec on your shell (by including it in the arguments to exec), based on the value in /etc/passwd.

Interesting this did not get more votes. It’s the only answer that actually specifies where the environment variable is set, which was the OP’s actual question.

Edit this by running: usermod -d /home/whatever_dir whatever_user .

Please note that this will (obviously) be the new home directory. Bash will cd to it on login, so make sure it exists and the permissions are correct. In addition, don’t forget about .bashrc , .profile , .xinitrc , etc; if they’re not in the home directory, they will not be read.

Usage: usermod [options] LOGIN Options: -c, --comment COMMENT new value of the GECOS field -d, --home HOME_DIR new home directory for the user account -e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE -f, --inactive INACTIVE set password inactive after expiration to INACTIVE -g, --gid GROUP force use GROUP as new primary group -G, --groups GROUPS new list of supplementary GROUPS -a, --append append the user to the supplemental GROUPS mentioned by the -G option without removing him/her from other groups -h, --help display this help message and exit -l, --login NEW_LOGIN new value of the login name -L, --lock lock the user account -m, --move-home move contents of the home directory to the new location (use only with -d) -o, --non-unique allow using duplicate (non-unique) UID -p, --password PASSWORD use encrypted password for the new password -R, --root CHROOT_DIR directory to chroot into -s, --shell SHELL new login shell for the user account -u, --uid UID new UID for the user account -U, --unlock unlock the user account 

Источник

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