My Computer Tips

Home | About | Categories | All Tips & Tutorials

Getting user input bash scripts

ID: 314

Category: Bash Scripts

Added: 2nd of August 2022

Views: 1,731

The following bash script gets the user input by using the read command and then printing out the results in the terminal.

If this is your first time creating a bash script please visit my other tutorial Create a simple bash script

Copy and past the code below in to your text editor, save the file as user_input.sh

#!/usr/bin/bash

echo "What is your name?"
read name
echo "How old are you?"
read age
echo "Hello $name, you are $age years old"


Make the file executable by entering the following command
chmod +x user_input.sh


Run the script by entering
./user_input.sh

Related Tips & Tutorials

Create a simple bash script in Ubuntu and Ubuntu based distributions