Reading User Input

Interactive user input is a vital feature if your scripts are going to provide rich functionality. To facilitate this sort of data capture, we use the read command:

Leveraging the read command to capture user input
#!/bin/bash

echo "What is your name"

read name

echo "Hello $name"

You can also use the -p and -sp options to set a prompt or silent prompt as necessary

Example Script
#!/bin/bash

read -p 'Username: ' username
read -sp 'Password: ' password

echo "Hello $username, your password is now set to $password"
Example of a script using -p and -sp with read

Last updated

Was this helpful?