Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Linux Shell Scripting

You're reading from   Mastering Linux Shell Scripting A practical guide to Linux command-line, Bash scripting, and Shell programming

Arrow left icon
Product type Paperback
Published in Apr 2018
Publisher
ISBN-13 9781788990554
Length 284 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Mokhtar Ebrahim Mokhtar Ebrahim
Author Profile Icon Mokhtar Ebrahim
Mokhtar Ebrahim
Andrew Mallett Andrew Mallett
Author Profile Icon Andrew Mallett
Andrew Mallett
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. The What and Why of Scripting with Bash FREE CHAPTER 2. Creating Interactive Scripts 3. Conditions Attached 4. Creating Code Snippets 5. Alternative Syntax 6. Iterating with Loops 7. Creating Building Blocks with Functions 8. Introducing the Stream Editor 9. Automating Apache Virtual Hosts 10. AWK Fundamentals 11. Regular Expressions 12. Summarizing Logs with AWK 13. A Better lastlog with AWK 14. Using Python as a Bash Scripting Alternative 15. Assessments 16. Other Books You May Enjoy

Declaring variables

Just like in any programming language, you can declare variables in bash scripts. So, what are these variables and what are the benefits of using them?

Well, a variable is like a placeholder where you store some value for later use in your code.

There are two kinds of variables you can declare in your script:

  • User-defined variables
  • Environment variables

User-defined variables

To declare a variable, just type the name you want and set its value using the equals sign (=).

Check out this example:

#!/bin/bash 
name="Mokhtar" 
age=35 
total=16.5 
echo $name  #prints Mokhtar 
echo $age   #prints 35 
echo $total #prints 16.5 

As you can see, to print the variable's value, you should use the dollar sign ($) before it.

Note that there are no spaces between the variable name and the equals sign, or between the equals sign and the value.

If you forget and type a space in between, the shell will treat the variable as if it were a command, and, since there is no such command, it will show an error.

All of the following examples are incorrect declarations:

# Don't declare variables like this: 
name = "Mokhtar" 
age =35 
total= 16.5 

Another useful type of user-defined variable is the array. An array can hold multiple values. So, if you have tens of values you want to use, you should use arrays instead of filling your script with variables.

To declare an array, just enclose its elements between brackets, like this:

#!/bin/bash 
myarr=(one two three four five) 

To access a specific array element, you can specify its index like this:

#!/bin/bash 
myarr=(one two three four five) 
echo ${myarr[1]} #prints two which is the second element 

The index is zero based.

To print the array elements, you can use an asterisk, like this:

#!/bin/bash 
myarr=(one two three four five) 
echo ${myarr[*]} 

To remove a specific element from the array, you can use the unset command:

#!/bin/bash 
myarr=(one two three four five) 
unset myarr[1] #This will remove the second element 
unset myarr    #This will remove all elements 

Environment variables

So far, we have used variables that we didn't define, such as $BASH_VERSION, $HOME, $PATH, and $USER. You might wonder, as we didn't declare these variables, where did they come from?

These variables are defined by the shell for your use and they are called environment variables.

There are many environment variables. If you want to list them, you can use the printenv command.

Also, you can print a specific environment variable by specifying it to the printenv command:

$ printenv HOME

We can use any of these variables in our bash scripts.

Note that all environment variables are written in capital letters, so you can declare your variables as lower case to make it easy to differentiate your variables from environment variables. This is not required, but is preferable.

You have been reading a chapter from
Mastering Linux Shell Scripting - Second Edition
Published in: Apr 2018
Publisher:
ISBN-13: 9781788990554
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image