0% found this document useful (0 votes)
11 views

Session - 11 (Shell Variables)

The document provides an overview of shell variables in Linux, detailing their types, usage, and manipulation. It distinguishes between system-defined and user-defined variables, explains how to declare, view, unset, and export them, and discusses specific variables like $SHELL and $PATH. Additionally, it includes practice questions to reinforce understanding of shell variable concepts.

Uploaded by

raor41101
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Session - 11 (Shell Variables)

The document provides an overview of shell variables in Linux, detailing their types, usage, and manipulation. It distinguishes between system-defined and user-defined variables, explains how to declare, view, unset, and export them, and discusses specific variables like $SHELL and $PATH. Additionally, it includes practice questions to reinforce understanding of shell variable concepts.

Uploaded by

raor41101
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Shell Variables in Linux

Shell Variables in Linux


Shell variables are entities used to store data such as strings, numbers, or paths. They are
essential in scripting and managing shell environments. These variables can be categorized into
two main types:
1. System-Defined Variables (Environment Variables)
• These are predefined variables created and maintained by the operating system.
• They affect the behaviour of the shell and various programs.
• They are typically written in uppercase (e.g., $HOME, $PATH, $USER).
• Examples:
# echo $HOME # Displays the home directory of the current user
# echo $PATH # Shows directories where the system searches for executables
• System-defined variables can be listed using:
# printenv # Displays all environment variables
# set # Lists all variables
# env # Shows exported variables

2. User-Defined Variables
• These are created manually by users for storing temporary data.
• By default, user-defined variables are only available in the current shell.
• Naming rules:
o Can contain letters, numbers, and underscores (_).
o Cannot start with a number.
o Should not have spaces around the equal sign.
• Example:
# my_var="Hello, Linux!"
# echo $my_var # Output: Hello, Linux!
• User-defined variables can be exported to child processes using:
# export my_var
Shell Variables in Linux

1.1 Variables in the Shell


Variables in the shell always begin with a dollar sign "$". They enable applications to define
specific settings, such as a directory path.
In this chapter, we will explore variables in the shell.

1.2 Declaring Variables


A variable can be defined by declaring it. The following example demonstrates how to declare
two variables: one explicitly and the other implicitly.
vikasnehra@localhost:~$ declare var1=33
vikasnehra@localhost:~$ var2=42
You can use the echo command to display the values of these variables:
vikasnehra@localhost:~$ echo The answer is $var2
The answer is 42
vikasnehra@localhost:~$ echo Why $var1 seconds
Why 33 seconds
Additionally, variables can store string values, such as directory paths:
vikasnehra@localhost:~$ var3=/var/tmp
vikasnehra@localhost:~$ cd $var3
vikasnehra@localhost:/var/tmp$ pwd
Note: When declaring a variable, do not use the dollar sign $.

1.3 Variables and Quotes


Variables enclosed within double quotes are interpreted by the shell and replaced with their
values:
vikasnehra@localhost:~$ echo "The answer is $var2"
The answer is 42
However, when enclosed in single quotes, variables are not interpreted:
vikasnehra@localhost:~$ echo 'Why $var1 seconds?'
Why $var1 seconds?
Note: Single quotes are often referred to as full quotes.
Shell Variables in Linux

1.4 Shell Parsing of Variables


The Bash shell replaces variables with their values. The following example demonstrates this
behavior with xtrace enabled:
vikasnehra@localhost:~$ set -x
vikasnehra@localhost:~$ echo "The answer is $var2"
+ echo 'The answer is 42'
The answer is 42
The echo command does not process $var2 directly; instead, it receives 42 from the Bash shell.

1.5 Escaping the Dollar Sign ($)


To prevent variable interpretation, escape the dollar sign with a backslash:
vikasnehra@localhost:~$ echo Why \$var1 seconds?
Why $var1 seconds?

1.6 Viewing Variables with set


To list all shell variables, use the set command. The output is extensive, so we limit it to the
first nine lines:
vikasnehra@localhost:~$ set | head -9
To find specific variables, use grep in combination with set:
vikasnehra@localhost:~$ set | grep var1
var1=33
vikasnehra@localhost:~$ set | grep var2
var2=42

1.7 Unsetting Variables


Variables can be removed using the unset command:
vikasnehra@localhost:~$ unset var2
Attempting to use an unset variable returns an empty value:
vikasnehra@localhost:~$ echo The answer is $var2
The answer is
Warning: Do not use the $ symbol when unsetting a variable.
Shell Variables in Linux

1.8 Handling Unbound Variables


To receive an error message when referencing an undefined variable, enable the nounset shell
option:
vikasnehra@localhost:~$ set -o
vikasnehra@localhost:~$ set -o nounset
vikasnehra@localhost:~$ echo The answer is $var2
-bash: var2: unbound variable
OR
vikasnehra@localhost:~$ set -u
vikasnehra@localhost:~$ echo $MyVar
bash: MyVar: unbound variable
vikasnehra@localhost:~$ set +u
vikasnehra@localhost:~$ echo $MyVar
vikasnehra@localhost:~$

1.9 Delimiting Variables


If a variable is part of a larger word, enclose it in curly braces:
vikasnehra@localhost:~$ pre=Super
vikasnehra@localhost:~$ echo ${pre}man is stronger than $pre’girl’
Superman is stronger than Supergirl

1.10 The $SHELL Variable


The $SHELL variable is initialized when the shell starts and typically holds /bin/bash:
vikasnehra@localhost:~$ echo $SHELL
/bin/bash

1.11 The $PS1 Variable


The $PS1 variable customizes the shell prompt. Below, we set the prompt to prompt>:
vikasnehra@localhost:~$ PS1='prompt>'
Shell Variables in Linux

prompt>
We can further modify it using predefined shortcuts:
vikasnehra@localhost:~$ PS1='\u@\h:\w@\t\$ '
vikasnehra@localhost:~@07:34:21$

1.12 The $PATH Variable


The $PATH variable contains directories searched for executable commands:
vikasnehra@localhost:~$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
To add a directory to $PATH, append it as follows:
vikasnehra@localhost:~$ PATH=$PATH:/home/vikasnehra/bin
To include the current directory (not recommended for security reasons), prepend .:
vikasnehra@localhost:~$ PATH=.:$PATH
Note: The current directory is represented by a single dot (.).

1.13 The env Command


The env command displays a list of exported variables. Unlike set, which lists all variables,
env only shows those exported to child shells. It can also be used to start a clean shell:
vikasnehra@localhost:~$ env -i bash -c 'echo $SHELL $HOME $USER'
/bin/bash
Additionally, env allows setting variables for a single command:
vikasnehra@localhost:test$ env LANG=C bash -c 'ls File[a-z]'
Filea Fileb
vikasnehra@localhost:test$ env LANG=en_US.UTF-8 bash -c 'ls File[a-z]'
Filea FileA Fileb FileB

1.14 The export Command


The export command allows shell variables to be inherited by child shells:
vikasnehra@localhost:~$ var4=four
vikasnehra@localhost:~$ export var4
Shell Variables in Linux

However, it does not pass variables to parent shells:


vikasnehra@localhost:~$ export var5=five
vikasnehra@localhost:~$ exit
vikasnehra@localhost:~$ echo $var5
This chapter provides an overview of shell variables, their behavior, and how to manipulate
them effectively.

PRACTICE QUESTIONS WITH SOLUTION:


1. Declare a variable named $count with a value of 500.
# count=500
2. Declare a variable named $mydir with a value of /opt/myfolder.
# mydir="/opt/myfolder"
3. Display both your variables on one line.
# echo "$count $mydir"
4. Display both your variables on one line with six spaces between them.
# echo "$count $mydir"
5. Display $count=500 by escaping the first dollar sign.
# echo "\$count=$count"
6. Use the set command to display your variables.
# set | grep -E 'count|mydir'
7. Delete the $mydir variable.
# unset mydir
8. Make sure you get an error when using $mydir.
# echo "$mydir"
9. Remove the /usr/games directory from the $PATH.
# export PATH=$(echo $PATH | sed -e 's|:/usr/games||')
10. Log out and log back on. Are your variables still there?
o If they were declared temporarily in the shell, they won’t persist.
o To make them permanent, add them to ~/.bashrc or ~/.bash_profile.

You might also like