The following will prompt a user for input, and then store that input as a string (text) in a variable. The variable is then used to give a message to the user.
#!/usr/bin/env bash
echo "Who are you?"
read name
echo "Hello, $name."
The command read
here reads one line of data from standard input into the variable name
. This is then referenced using $name
and printed to standard out using echo
.
Example output:
$ ./hello_world.sh
Who are you?
Matt
Hello, Matt.
Here the user entered the name "Matt", and this code was used to say Hello, Matt.
.
And if you want to append something to the variable value while printing it, use curly brackets around the variable name as shown in the following example:
#!/usr/bin/env bash
echo "What are you doing?"
read action
echo "You are ${action}ing."
Example output:
$ ./hello_world.sh
What are you doing?
Sleep
You are Sleeping.
Here when user enters an action, "ing" is appended to that action while printing.