Jump to content

Bash programming/Interactive input

From Wikiversity

Taking user input is invaluable when writing an application. Receiving and interpreting a user's input is a simple task in Bash scripting. All that is needed is to declare a variable to read, and use it for something. An easy way to explain this would be to ask for a user's name, and display it. We will use read built-in command[1]:

echo "Enter your name please and press ENTER:"
read NAME
echo "Hi there ${NAME}, pleased to meet you."

In this simple program, there is the command echo, which prints out a string (the part in quotes), as well as a newline. read gets user input (terminated by a newline), and stores this input into the variable NAME. In the final echo, the ${NAME} is used to tell bash that it's value should be inserted in the string at that location.

See also

[edit | edit source]
  • printf and echo built-in commands

References

[edit | edit source]