amazon media file explorer download upload index folder image licenses menu widget Play Pause profile-settings more dots-two-horizontal dots-two-vertical more-vertical pending google-plus hangouts facebook instagram whatsapp spotify telegram twitter vine renren rss youtube twitch vimeo flickr dribble behance deviantart 500px steam github soundcloud skype reddit linkedin lastfm delicious stackoverflow pinterest xing flattr foursquare yelp World

Using read in a Bash Script

We previously looked at using echo with options in a shell, in this guide we would combine it with read command to prompt for user input.

Create and open up a new text file: nano $HOME/bin/hw3.sh

Add the following:

#!/bin/bash
echo -n "Hello There, may I ask your name: "
read
echo "Hello $REPLY"
exit 0

Let's understand the command line by line:

#!/bin/bash  - This is the interpreter, nothing new here

read - This reads a line from the standard input (e.g your keyboard input), you are to pass a variable to the read command. If you do not, it is going to use the default internal variable, which is the $REPLY variable.

echo "Hello $REPLY" - The $REPLY  is the default value when a variable is not supplied to read, it would store whatever you type in the stdin, and we then echo it.

Remember to add an execution bit to the file: chmod +x $HOME/bin/hw3.sh

This the output:

user@server:~/bin$ hw3.sh
Hello There, may I ask your name: ? Mr. Devsrealmer
Hello Mr. Devsrealmer
user@server:~/bin$

In future guide, we would talk more on the read builtin command

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

  • 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

  • grep (Regular Expression) In Bash Scripting

    We recently discussed filters in bash scripting, in this guide we would look more into the practical usage of using a filter program, and an example of such a program is grep. grep, in its simplest f

  • Sort in Bash Scripting

    We recently just looked at grep in bash scripting, and another commonly used program in bash scripting is the program called sort. Sort is a simple filter that is used to sort lines of text from its