User input (Bash)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

Direct mode

Interestingly, man read doesn't exist, but you can use read in direct mode:

$ read blub; echo $blub

hallo ← User input
hallo ← Echod back to console

Simple script

read is probably more relevant in a script than in direct mode:


read response
echo "Response: $response

When executing this script, there is just a blinking prompt. Input is subsequently echod.

Including prompt


read -p "Please type something" response
echo "Response: $response

The flag -p indentifies the next argument as prompt. Without this flag, an error would occur.

Including silent prompt

Nice of asking for e.g., a password:


read -ps "Password?" response

read usage

No man page seems to exit, but sometimes (like through read -help) some help can be get:

read: usage: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]