facebook youtube pinterest twitter reddit whatsapp instagram

read prompts in bash script

We've previously taken a quick look at the builtin read command, where we use the built-in read to populate the $REPLY variable, which holds the value of read when a variable is not supplied to read, it would store whatever you type in the stdin, and we then echo it.

In this guide, we would use the read to produce a prompt, and a way to supply read a variable.

Fire up your console and type the following command:

read -p "What is Your Name: " name

The syntax is using a read with the -p option, and you include the string that appear in the prompt. The last argument (name) is the variable we want to populate, so whatever input the user type is stored in this variable.

This is the output of the above command, go ahead and type in a string or your name:

user@server:~/bin$ read -p "What is Your Name: " name
What is Your Name: Mr. Devsrealmer

Now, the variable name has the value you typed in memory, you can echo it as follows:

user@server:~/bin$ echo "You typed $name"
You typed Mr. Devsrealmer

Cool right! You can turn the whole thing into a simple script:

#!/bin/bash
read -p "What is Your Name? " name
echo "Your Name is $name"
exit 0

Related Post(s)

  • sed (Stream Editor) In Bash Scripting

    Another powerful filter in GNU/Linux is sed (stream editor), which is a program for performing basic editing tasks on the output of another program or on a file. sed or stream editor can be used to p

  • awk In Bash Scripting

    Up until now, we have covered a couple of text processing, from sed to grep to sort, and now we have the big daddy, which is awk. awk (which stands for Aho, Weinberger, and Kernighan - its creators)

  • (Bash Script) Automate Multiple Classicpress & Wordpress Installation + [Caching and SSL]

    If you are tired of using control panels (I am because they are mostly not secure, and most comes loaded with bundles of useless stacks), and want to do everything on the server level, I wrote a menu

  • Returning Values From Functions in (Bash)

    In the guide function in bash, we dive deep in bash functions and we also pass some information into the functions to carry out a certain operation. In this guide, I'll show you how to get informatio

  • Functions in Bash

    Functions are modular building blocks for creating powerful and modular scripts, using function makes it easy to isolate a code since it would be in a single building block, which would be isolated f

  • Understanding The Four (4) Ways Command-Line Runs Shell Script

    Sometimes we need to think like the command line shell in order to understand how things are done in the background, this would make debugging a breeze. In this guide, you would learn all the variou