User input (Bash)

Uit De Vliegende Brigade
Versie door Jeroen Strompf (overleg | bijdragen) op 21 jul 2022 om 12:48
(wijz) ← Oudere versie | Huidige versie (wijz) | Nieuwere versie → (wijz)
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:

#!/bin/bash

read response
echo "Response: $response

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

Including prompt

#!/bin/bash

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:

#!/bin/bash

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

Sources