Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Read User Input
2.1.
Example 1
2.2.
What will happen if you don't pass any variable with a read command?
2.3.
Example 2
2.4.
Multiple inputs using an array
3.
Frequently Asked Questions
3.1.
What do you mean by a shell?
3.2.
Define metacharacter.
3.3.
Which command we can use in place of echo?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Shell Scripting - Read User Input

Author Ashish Sharma
1 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Bash is a sh-compatible command language interpreter that runs commands from standard input or a file. Bash also borrows functionality from the Korn and C shells (ksh and csh).

Bash is a command processor that normally runs in a text window in which the user writes commands that cause actions to be performed. Bash can also read and execute commands stored in a file known as the shell script.

In this article, we will discuss Bash language, how to get the user input, how to take input in the bash console, and examples related to it.

Read User Input

If we want to request input from the user, we will use the read command. This command accepts input and saves it to a variable. It reads a single line from the Bash shell. 

Now let’s understand the basic keywords of bash commands so that we can learn Shell Scripting - Read User Input:

Commands Description
Echo It displays the argument on standard output.
pwd(print working directory) It displays the current working directory.
ls(list) It lists the content of the directory.
cd(change directory) It is used to change the directory.
mkdir(make directory) It is used to make a directory.
rmdir(remove directory) It is used to remove the directory.
touch It is used to create blank files.
rm(remove) It removes the files from a directory.
mv(move) It is used to move the file from one directory to another.
p It is used to prevent shell reading from user-controlled files.
$ It is used to print the variable values.
sp It is used to keep the user information hidden.

 

You can run the bash command in an online code editor like replit.

Syntax

read<variablename>

Example 1

# example to show Shell Scripting - Read User Input
# Asking the user for their name
echo Hello Ninja what is your name
read varname
echo It\'s nice to meet you $varname

 

Output

Output

Explanation 

In line 2 of the example, you can see that we are printing a statement. In the next line, we ask the user to write their name.

Finally, we print the user name with an echo command in the third line. You can see that we have used a ‘\’ in the statement. This is because we want extra space between the letters.

What will happen if you don't pass any variable with a read command?

If you don't pass any variable with a read command, then we can pass the built-in variable called REPLY for displaying the input. Let’s understand it with an example.

Let’s consider the above example. We need to use the REPLY function for writing without a variable, which gets the user input without any variable as it reads the last line as whole input from the user.

#Example to show Shell Scripting - Read User Input
# Asking the user for their name
echo Hello Ninja coder, my name is ashish and what is yours?
read 
echo It\'s nice to meet you "$REPLY"

 

Output

Output

Example 2

Let’s understand how we can get Shell Scripting - Read User Input using p and sp command. We will use sp command because it hides the user information. You can see it in the below output.

Syntax

p<Variablename>

#Example to show Shell Scripting - Read User Input
#!/bin/bash
# Ask the user for login details
read -p 'Username: ' user
read -sp 'Password: ' pass
echo
echo Thankyou $user we now have your login details now

 

Output

Output

Multiple inputs using an array

To take input as an array, we will use ‘-a’ command which helps us to take multiple inputs from the user.

#Example to show Shell Scripting - Read User Input
#!/bin/bash  
  
# Reading multiple inputs using an array  
  
echo "Enter the names : "  
read -a name  
echo "Entered names are : ${name[0]}, ${name[1]}." 

 

Output

Output

 

Now the question arises that we have seen two methods:

🔺Command line arguments.

🔺Read inputs during script execution
 

Which one is better in Shell Scripting - Read User Input?

Command-line arguments should be the first priority as they are the most convenient for users. This is because the data is saved in their command history, allowing them to return to data quickly to the user.

But in this case, if you want to take the inputs like passwords, then reading inputs during script execution will be the better option as it hides the data from being displayed.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What do you mean by a shell?

Shell is a command-line interpreter that converts user-entered commands into kernel-comprehensible language. The shell evaluates the command entered at the terminal and calls the appropriate program.

Define metacharacter.

A metacharacter is a unique character that is incorporated into a shell's program or data field. It provides information on other characters. 

Which command we can use in place of echo?

Tput is an alternative command for echo. We can use this command to control how the output is shown on the screen.

Conclusion

In this article, we will discuss Bash language, how to get the user input, how to take input in the bash console, and examples related to it.

After reading about Shell Scripting - Read User Input, are you not feeling excited to explore more articles on Shell Scripting? Don't worry; Coding Ninjas has you covered. If you want to check out java-related articles, refer to the Introduction to Linux Shell & Scripting ShellShell Scripting Interview QuestionsCrontab in Linux.

You can refer to the Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass