-1

What does the -p option do in the read command?

An example:

read -p "Please enter your first name: " firstName

An explanation of the -p option on read's manual page (I don't understand this):

output the string PROMPT without a trailing newline before attempting to read

I don't understand what 'the string PROMPT' means, nor do I understand what 'a trailing newline' is.

Kaiylar
  • 627
  • I don't understand what 'the string PROMPT' means, nor do I understand what 'a trailing newline' is, so I don't understand what -p does to the read command. – Kaiylar Mar 14 '16 at 23:16

2 Answers2

4

I don't understand what 'the string PROMPT' means, nor do I understand what 'a trailing newline' is.

Prompt is how the string given after -p is called. User is presented with a prompt (Please enter your first name: and enters value that will be put in firstName variable.

Without trailing newline means that when user starts typing, the characters will appear on the screen (will be echoed to) right after Please enter your first name:in the same line (as opposed to appearing below the prompt).

techraf
  • 5,941
2

Running the example would show you. The script will display

Please enter your first name: 

and your terminal's cursor would be one space past the ":" (on the same line). If you type anything, and press enter, the text is put into the shell variable $firstName

Please enter your first name: Kaiylar

sets the variable to "Kaiylar". In the script you could type

echo $firstName

and see

Kaiylar

In the manual, PROMPT is a name for the string

"Please enter your first name: "

and newlines are the character ending the line of text and starting a new line of text.

Thomas Dickey
  • 76,765