Getting input from terminal using bash script – Code Example

In this article we will provide you bash script code for getting input from terminal. We will use read command to read the value provided by user.

Code Example –

1. Reading input –

#!/bin/bash

echo "Enter your name: "
read -r name
echo "Your name: $name"

# Output:
# Enter your name: Akash Mittal
# Your name: Akash Mittal

2. Showing input prompt –

#!/bin/bash

read -p 'Username: ' username
echo "Your Username: $username"

# Output:
# Username: akku
# Your Username: akku

3. Silent input prompt – Then entered value will not be visible. Best for asking passwords.

#!/bin/bash

read -sp 'Password: ' password
echo "Your Password: $password"

# Output:
# Password:
# Your Password: my_secure_pass