[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedJuly 18, 2023

How to understand Environment Variables in Bash

How To Understand Environment Variables In BASH

Environment variables play a crucial role in the BASH shell, allowing you to customize and configure your shell environment. In this article, we’ll explore various examples of how to work with environment variables in BASH.

  1. Viewing Environment Variables: To view all the environment variables currently set in your BASH session, you can use the env command or printenv command. For example:

$ env
$ printenv
  1. Setting Environment Variables: You can set environment variables using the export command. Here’s an example:

$ export MY_VARIABLE="Hello, World!"
  1. Accessing Environment Variables: To access the value of an environment variable, you can use the $ symbol followed by the variable name. For example:

$ echo $MY_VARIABLE
  1. Default Values: If an environment variable is not set, you can provide a default value using the ${VAR_NAME:-default} syntax. Here’s an example:

$ echo ${UNKNOWN_VARIABLE:-"No value found."}
  1. Modifying Environment Variables: You can modify the value of an environment variable using the = operator. For example:

$ MY_VARIABLE="New value" 
  1. Environment Variables in Scripts: To use environment variables in a BASH script, you can export them before running the script. For example:

$ export MY_VARIABLE="Hello, World!"
$ ./my_script.sh
  1. PATH Variable: The PATH variable specifies the directories where the shell looks for executable files. You can append directories to the PATH variable to make your scripts or programs accessible. For example:

$ export PATH=$PATH:/path/to/new_directory
  1. HOME Variable: The HOME variable stores the home directory of the current user. You can use it to refer to the home directory in your scripts. For example:

$ echo $HOME
  1. USER Variable: The USER variable holds the username of the current user. You can use it to perform actions based on the current user. For example:

$ echo $USER
  1. PS1 Variable: The PS1 variable defines the format of your shell prompt. You can customize it to display useful information. For example:

$ export PS1="\u@\h:\w\$ "

 

Conclusion:

Understanding and utilizing environment variables in BASH can greatly enhance your command-line experience. With the examples provided in this article, you should now have a better grasp of how to work with environment variables.

Keep reading
How to use pushd & podp command.
March 15, 2023

How to use pushd & podp command.

How To Use Pushd & Podp Command. In this tutorial, we will show you how to use the pushd and popd commands to navigate your system’s directory tree. pushd and popd are commands that allow you to work with directory stack and change the current working directory in Linux and other Unix-like…

Read article
How to use Gunzip Command.
March 15, 2023

How to use Gunzip Command.

How To Use Gunzip Command. Gunzip is a command-line tool for decompressing Gzip files. Gzip is one of the most popular compression algorithms that reduce the size of a file and keep the original file mode, ownership, and timestamp. By convention, files compressed with Gzip are given either the .gz…

Read article
How to use Chmod Command.
March 15, 2023

How to use Chmod Command.

How To Use Chmod Command. This tutorial covers how to use the chmod command to change the access permissions of files and directories. In Linux, access to the files is managed through the file permissions, attributes, and ownership. This ensures that only authorized users and processes can access…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00