Table of Contents
- Introduction
- How to create string variables in Bash scripts
- How to use the created variables in the bash script
- How to do parameter expansion in the bash variables
- How to use the length of the variable in the Bash script
- How to create the variables using user inputs
Introduction
Wondering how to create and use variables in your bash script ? This article covers the process in detail with examples.
In Bash the variables are dynamically Typed , which means its type is treated as string or the integer based on the context during the runtime. In this article we will be dealing more with the string dynamically typed variables
How to create string variables in Bash scripts
In the following section we will be learning what is the right way to create the variables in the bash script
There is an issue with the following script where the variable “name” is not working as expected. This is because due to syntax issue Bash considers “name” as a command instead of the variable due to the space next to that
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
name = "DS admin"
echo "Hello $name!"
[root@discoveringsystems Bash-Scripting]# ./script.sh
./script.sh: line 3: name: command not found
Hello !
To fix it we need to use the following syntax without spaces
name="DS admin"
After fixing the syntax issues, it works as per expectation
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
name="DS admin"
echo "Hello $name!"
[root@discoveringsystems Bash-Scripting]# ./script.sh
Hello DS admin!
To summarize ,
Following are the wrong ways to declare a variable in bash
name= "DS admin" # returns the error DS admin: command not found
name ="DS admin" # returns the error name: command not found
name = "DS admin" # returns the error name: command not found
Here is the right way to declare a variable in bash scripts
name="DS admin"
How to use the created variables in the bash script
In the following simple bash script, I used $name to use the already created variable
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
name="DS admin"
echo "Hello $name!"
[root@discoveringsystems Bash-Scripting]# ./script.sh
Hello DS admin!
Now I changed the echo command to use single quotes instead of double quotes. This made the variable call not working as per expectation, we need to use the “double Quotes” instead of the ‘single Quotes’ to make it work when using echo to print the variable “name” declared in the script
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
name="DS admin"
echo 'hello $name!'
[root@discoveringsystems Bash-Scripting]# ./script.sh
hello $name!
How to do parameter expansion in the bash variables
String substitution in Bash variables
In the following example i have substituted a part of the string “admin” with “user” in the variable “name”
Syntax : ${variable/actual_string/substitution_string}
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
name="DS admin"
echo "hello ${name/admin/user}!"
[root@discoveringsystems Bash-Scripting]# ./script.sh
hello DS user!
How to use only a portion from the Bash variable using Substring expansion concept
Using the substring expansion concept we can use a part from the variable in our scripts. There are two parameters used in this concept called position parameter and the length parameter. Position parameter sets the pointer to a start character of the variable string. The length parameter denotes starting from the character pointed by the position parameter ,(i.e) how many characters can be used after the position parameter. Using the following syntax from the position parameter it considers the string characters until the length parameter
Syntax : ${string_variable:position:length}
# here in this example , i am using only the “DS” from the variable “name” using the Substring expansion concept
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
name="DS admin"
echo "hello ${name:0:2}!"
[root@discoveringsystems Bash-Scripting]# ./script.sh
hello DS!
Let’s say we would like to modify the same script and use only admin instead of DS , we can do the following modifications
# without length parameter , next to position parameter it uses the string starting from the positional parameter until the end of string variable
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
name="DS admin"
echo "hello ${name:3}!"
[root@discoveringsystems Bash-Scripting]# ./script.sh
hello admin!
Or
# with length parameter , next to position parameter it uses the string starting from the positional parameter until the length specified and produces the same result like the previous example
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
name="DS admin"
echo "hello ${name:3:5}!"
[root@discoveringsystems Bash-Scripting]# ./script.sh
hello admin!
Or
This is an another variation of the Substring expansion
In this case we are using the last 5 characters from the variable “name” using the following
Syntax: ${variable: -5}
In the example I am using the last 5 characters “admin” from the “DS admin” to produce the same results like earlier examples
#working script
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
name="DS admin"
echo "hello ${name: -5}!"
[root@discoveringsystems Bash-Scripting]# ./script.sh
hello admin!
# Make sure that there is a space between the -5 and ‘:’ as without that it won’t work as you see in the following wrong output example . this example script below not working as expected due to mistake pointed out
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
name="DS admin"
echo "hello ${name:-5}!"
[root@discoveringsystems Bash-Scripting]# ./script.sh
hello DS admin!
How to use the length of the variable in the Bash script
In the following script , I am using only the length of the variable , instead of the actual variable itself
Syntax: ${#variable}
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
name="DS admin"
echo "the length of the variable name is ${#name}!"
[root@discoveringsystems Bash-Scripting]# ./script.sh
the length of the variable name is 8!
How to create the variables using user inputs
We are using the "read
” command to get the user inputs and save it as the "var_name
” variable and later using it in the script
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
echo "Please print your name"
read var_name
echo "Hello ${var_name}!"
echo "How may I help you !"
[root@discoveringsystems Bash-Scripting]# ./script.sh
Please print your name
super user
Hello super user!
How may I help you !
Let’s say you don’t want to use echo to initially ask the user for inputs , “read”
command provides the feature to prompt with -p
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
read -p "Please print your name : " var_name
echo "Hello ${var_name}!"
echo "How may I help you !"
[root@discoveringsystems Bash-Scripting]# ./script.sh
Please print your name : Super user
Hello Super user!
How may I help you
Sometimes the user might not provide the details or provide empty values and hit the return , in that case we can use some default values as the user provided empty values.
In the following script , using this syntax : ${var_name:-"Anonymous user"}
we were able to substitute the default values when the user provided values in the read command prompt is empty
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
read -p "Please print your name : " var_name
echo "Hello ${var_name:-"Anonymous user"}!"
echo "How may I help you !"
[root@discoveringsystems Bash-Scripting]# ./script.sh
Please print your name :
Hello Anonymous user!
How may I help you !
This works for null values (var_name=)
as well
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
var_name=
echo "Hello ${var_name:-"Anonymous user"}!"
echo "How may I help you !"
[root@discoveringsystems Bash-Scripting]# ./script.sh
Hello Anonymous user!
How may I help you !
This works for Empty string (var_name=""
) as well
[root@discoveringsystems Bash-Scripting]# cat script.sh
#!/usr/bin/env bash
var_name=""
echo "Hello ${var_name:-"Anonymous user"}!"
echo "How may I help you !"
[root@discoveringsystems Bash-Scripting]# ./script.sh
Hello Anonymous user!
How may I help you !