Interacting with the User

Simply assigning variables inside a script and calling those variables later is not very useful. The shell provides a way for you to get input from STDIN. Normally, this input will be typed in by the user (or received from a file if STDIN has been redirected). The command that can read input from a user is read. Here is a slightly modified version of the “Hello World” program that reads input from STDIN, which is normally the keyboard:

1.  #!/bin/sh
2.
3.  # Modified Hello World program that accepts input from keyboard
4.
5.  echo
6.  echo -n "Please enter your name: "
7.  read name
8.  echo
9.  echo "Hello, ${ name}!"
10. echo
11. exit 0

When run, this program does the following:

 Please enter your name: Mike Hello, Mike! ...

Get FreeBSD® Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.