Files
linux.softwareshinobi.com/landing/docs/Bash-Scripts/005-bash-user-input.md
Software Shinobi 7d9171c854
All checks were successful
learn org at code.softwareshinobi.com/linux.softwareshinobi.com/pipeline/head This commit looks good
reworking content
2025-06-19 10:03:08 -04:00

948 B

User Input

You've outputted variable values. Now, let's get input from the user.

First, create your script file:

touch shinobi.sh

Next, open shinobi.sh and add this:

#!/usr/bin/env bash

echo "What is your name?"
read name

echo "Hi there $name"
echo "Welcome to Shinobi!"

Save and exit the file.

Make it executable:

chmod +x shinobi.sh

Run your script:

./shinobi.sh

The script will prompt you:

What is your name?
Troy

Enter a name (e.g., Troy) and hit Enter. You'll see:

Hi there Troy
Welcome to Shinobi!

This script prompts the user, stores their input in the name variable, then uses it to print a personalized message.

Streamlined Input

To reduce lines, use read with the -p flag. This prints a prompt before waiting for input:

#!/usr/bin/env bash

read -p "What is your name? " name

echo "Hi there $name"
echo "Welcome to Shinobi!"