Enhancing scripts with read prompts

We have seen how we can use the built in read to populate a variable. So far, we have used echo to produce the prompt but this can be passed to read itself using the -p option. The read command will surpass the additional linefeed, so we reduce both the line count and the complexity to some degree.

We can test this at the command line itself. Try typing the following command to see read in action:

$ read -p "Enter your name: " name

We use the read command with the -p option. The argument that follows the option is the text that appears in the prompt. Normally, we will make sure that there is a trailing space at the end of the text to ensure that we can clearly see what we type. The last argument supplied here ...

Get Mastering Linux Shell Scripting 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.