BASH reading user input

Published: Saturday, 25 June 2011

You can get input from the user using the read command. It will save the user’s response into a variable.

#!/bin/sh

echo -n "what is your favourite fish? "
read FISH
echo you like $FISH

The above example asks the user to input their favourite fish, and echoes the answer back to them.

The -n switch causes echo to omit the newline.

what is your favourite fish? tuna
you like tuna

In the above example the user has typed in ’tuna'.