Up until now, we have only read about using variables that we have mentioned inside the script but that’s not a good way. We might be required to use a different value for a variable & we then need to update script each and every time. But there is a simple solution to this, taking user input with read command in the shell script.

Recommended Read: Install EPEL repository on CentOS/RHEL 6, 7 & 8

Also Read: Bash Scripting-9- using UNTIL & WHILE loop in shell script

Using read command in a shell script allows us to assign a user-entered value to a variable. Let’s show an example of how we can use read command in shell script,

#!/bin/bash

echo “ what is your name ?”

read var

echo “You are most welcome” $var

So, here we have not mentioned a variable beforehand and provided the name of the variable, just after read command. So now when the script is executed, it will ask for the name & then will assign the value to the variable named var. In the next step, it will print the welcome message along with the user-provided name.

Now let’s discuss a rather complex script than the one mentioned above,

#!/bin/bash

echo “ Please enter the full path of file/directory (ex- /home/ec2-user/new.txt)”

read path

for file in $path

do

               if [ -d “$file” ]

               then

                              echo “$file is a directory”

               elif [ -f “$file” ]

               then

                              echo “$file is a file”

               else

               echo “No file found”

done

So this script will take a directory or file path as input & based on the path provided, it will let us know whether the entered path is a file, directory or none of those.

We can also ask for some sensitive information using read command, for that we need to enter option ‘-s’, this option will hide the entered information,

# read -s variable_name

Now along with the read command, there is another way we can ask for user input from a bash script, using the following parameters,

$0, when used in script prints name of the script,

$1 will be the first value provided when running the script,

$2 will the second value provided when running the script,

$# will print the total number of values provided.

Let’s see an example of this as well,

#!/bin/bash

echo “Name of the script is $0”

echo “first value is $1”

echo “second value is $2”

echo “total numberof values are $#”

sum=$[$1 + $2]

echo “sum of $1 & $2 is $sum”

Now when we will execute the script, we must do like this,

# sh script.sh 5 15

Notice that we have mentioned 5 & 15 when executing the script. So, here $0 is the name of script i.e. ‘script.sh’, $1 is first number i.e. 5, $2 is the second 15. Similarly, we can add as variables in our script & call them when running the script.

We now end this tutorial on using read command in shell script. Please feel free to send in any questions, queries or suggestions using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.