Variable and environment variable

Bash Shell in Linux

1. Introduction

1.1. Variable

Variables are important components of every programming language and are used to hold different data. Scripting languages do not require to declare variable type first. In Bash shell, the value of variable is a string, even if we specify the value of that variable to be enclosed in double quotes or quotes or not. In addition, there are many variables used by the shell environment and the operating system environment to store default values, called environment variables.

Variables are named with the usual naming structure. When an application executes, it is passed a set of variables called environment variables. To view all environment variables related to the terminal, use the following command:

$ env

For each process, its runtime environment variables are viewable by:

$ cat /proc/$PID/environ

with PID is process ID of the process.

In order to get process ID of a process, use the following command:

$ pgrep process_name

Example:

$ pgrep init 
1

We can get the environment variables corresponding to the above process by executing the following command:

$ cat /proc/1/environ
HOME=/TERM=linuxPATH=/sbin:/bin:/usr/sbin:/usr/bin

A variable can be declared as below:

var = value

where var is the variable name and value is the value specified for the variable. If the value does not contain any space characters, it does not need to be enclosed in quotes or double quotes. Otherwise, it must be enclosed in quotes or double quotes.

Note, var = value and var=value have different meanings.

  • var=value is a assign operator..
  • var = value is the equality comparison operator.

In order to get value of variable, we add prefix $ before variable name,

Example:

var="value"
echo $var

or

echo ${var}

1.2. Environment variable

Environment variables are variables that are not defined in the current process, but are received from parent processes.

The export command is used to set the env variable. Any application that executed from the current shell script, will receive this variable. We can export custom variables for our own purposes in an application or an executed shell script. There are many default environment variables available to the shell.

Example, environment variable PATH

$ echo $PATH
/home/slynux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

When it receives a command to execute, the shell automatically looks for executable files in the list of directories in the PATH environment variable (directory paths are separated by the comma). Normally, $PATH is defined in /etc/environment or /etc/profile or ~/.bashrc. When we need to add a new path to the PATH environment variable, we use the following command:

$ export PATH="$PATH:/home/user/bin"

or

$ PATH="$PATH:/home/user/bin"
$ export PATH
$ echo $PATH
/home/slynux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/user/bin

Besides the PATH environment variable, some other common environment variables like HOME, PWD, USER, UID, SHELL…

2. Some tips with variable and environment variable

2.1. Get data length of a variable

In order to get data length of a variable, we can use the below command.

length=${#var}

Example:

var=12345678901234567890$
echo ${#var}
20

2.2. Determine the current shell

In order to determine the current shell, we can use the below command.

$ echo $SHELL
/bin/bash

or

$ echo $0
/bin/bash

2.3. Check current account

UID is an important environment variable that can be used to check whether the current script was run with the root or regular account.

Example:

if [ $UID -ne 0 ]; then
    echo Non root user. Please run as root.
else
    echo Root user.
fi

The value of environment variable UID for root account is 0.

2.4. Change the Bash prompt string ([email protected]:~$)

When we open a terminal window or run a shell, we see a prompt string like [email protected]:/home/$ for example. Different GNU/Linux distributions have different prompts and different colors. We can customize this prompt using the environment variable PS1. The default shell prompt is set to 1 used in the /etc/bashrc file

We can find that line by the below command:

$ cat /etc/bashrc |grep PS1
if [ "$PS1" ]; then
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\[email protected]\h \W]\\$ "
 # if [ "$PS1" ]; then
 # PS1="[\[email protected]\h:\l \W]\\$ "
 if [ "$PS1" ]; then

To set up another custom prompt, use the following command:

[email protected]: ~$ PS1 = "PROMPT>"
PROMPT> <the prompt was changed>

We can use colored strings using color codes (see echo and printf).

There are some special characters that being used for system parameters. For example \u for username, \h for hostname and \w for the current working directory.

SHARE:

Share on facebook
Facebook
Share on linkedin
LinkedIn
Share on email
Email

Recent Post

Follow us