You are currently viewing Shell Scripting – Environment Variables
Shell Scripting – Environment Variables

Shell Scripting – Environment Variables

Shell Scripting – Environment Variables

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed user-defined local variables in the shell.

https://cloudaffaire.com/shell-scripting-local-variables/

In this blog post, we will discuss user defined environmental variables. An environment variable is a persistent variable with different degrees of persistence used to store some value. There are different ways to set and unset an environment variable and they can be persistent across process within a session, across sessions for a single user or across the system. Apart from user-defined environment variables, the shell also store its own environment variable (shell variables).

Shell Scripting – Environment Variables

List environment variables:

You can list all environment variables using env command. You can also print a specific environment variables using printenv command.

Set environment variable:

The environment variable can be set using the export command.

Unset environment variable:

You can remove an environment variable using the unset command.

Environment variable scope:

When you create an environment variable using the export command, it is persistent across different processes within the current session only. If you want the environment variable to persist across sessions for a single user, you need to define the environment variable in .profile or .bash_profile or .bashrc files. If you want your environment variable to persist across the system, you need to add it in /etc/environment, /etc/profile, /etc/profile.d/, /etc/bash.bashrc files.

Hope you have enjoyed this article. In the next blog post, we will discuss shell variables.

 

Leave a Reply