-3

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.

  • 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

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).

  • And "trailing" means it is put on at the end. – Wildcard Mar 14 '16 at 23:26
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.