ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

Shell Script Chapter 2: Shell Script Variables

prodevelopertutorial February 12, 2019

In shell script there is no need to declare a variable before using it. A variable is a name given to memory location, which is used to store a value. The value can be accessed by variable name for further manipulation.

In this chapter we shall look at below topics:
2.1 How to set a variable?
2.2 How to access a shell variable?
2.3 Rules for defining a variable
2.4 Unsettling a variable
2.5 How to declare a read-only variable?
2.6 Environment variables
2.7 Shell Script arrays

2.1 How to set a variable?

Syntax:

<variable_name>=<value>

Example:

website_name=prodevelopertutorial.com

NOTE:

While setting a variable, there should not be any space between equals “=”. If there is a space, interpreter will throw an error.

Here “website_name” will hold the value “prodevelopertutorial.com”.

 

2.2 How to access a shell variable?

To access a variable, prefix the name with a “$” dollar symbol.

Example:

$website_name

Note:

If the string value you are assigning contains space, then it should be enclosed in single[”] or double [“”] quotes.

Example:

website_name=" prodevelopertutorial.com is a useful website"

or

website_name=' prodevelopertutorial.com is a useful website'

2.3 Rules for defining a variable:

1. Variable name can have alphanumeric characters.
2. Other than underscore (_), no other special characters are allowed in variable name.
3. A variable name can start with an alphabet or an underscore. It cannot start with number.

 

2.4 Unsettling a variable:

To unset the variable value, use “unset” keyword.

Syntax:

unset <variable_name>

Example:

unset website_name

 

2.5 How to declare a read-only variable?

To make a variable value to be read-only use “readonly” keyword. Once you make a variable read only, the value cannot be changed.

Syntax:

readonly <variable_name>

 

Example:
I have created “read_only.sh” file with below content:

#!/bin/bash

website_name=prodevelopertutorial.com

readonly website_name

echo $website_name

unset website_name

Output:

When I try to run it below error will be generated:

prodevelopertutorial.com
./read_only.sh: line 9: unset: website_name: cannot unset: readonly variable

 

2.6 Environment variables:

Till now we have seen Shell variables. These shell variables are local to the currently running shell. But in Linux like operating system, there exist a special variable that are available system wide, where the programs executing in shell can get the compile time information about its environment. These kind of variables are called as environment variables.

“printenv” command is used in shell to get the information about environment variables. Below is the output according to my system.

SHELL=/bin/bash
HISTSIZE=1000
USERNAME=root
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/usr/local/shell_script
LANG=en_US.UTF-8
HOME=/root
SUDO_COMMAND=/bin/bash
SHLVL=2
LOGNAME=root

To access these variables, you can directly use

echo $<envt_variable_name>

Example:

echo $SHELL

Output:

/bin/bash

To make a shell variable available as an Environment Variable, use “export <variable_name>” command. Without dollar sign.

Example:

website_name=prodevelopertutorial.com
export website_name

 

2.7 Shell Script arrays

In this chapter we shall understand how to use arrays.
Bash support one dimension arrays. An array is a sequence of elements. Like in other languages, array index starts from 0.

Example of array:

#! /bin/bash

# To declare Array
array=('Pro' 'Developer' 'Tutorial' 365 'days')

# Print all elements of array
echo ${array[@]}
echo ${array[*]}

#Print first element of the array
echo ${array[0]}
echo ${array}

#print any particular element
echo ${array[4]}
echo ${array[2]}

Output:

Pro Developer Tutorial 365 days
Pro Developer Tutorial 365 days
Pro
Pro
days
Tutorial

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT