User Input In Scripts
Part of the Fundamentals section of Coddy's Terminal journey — lesson 69 of 82.
Variables are useful, but sometimes you want your script to ask the user for information while it runs. The read command pauses the script and waits for the user to type something, then stores that input in a variable.
Here's how it works:
#!/bin/bash
echo "What is your name?"
read name
echo "Hello, $name!"When this script runs, it prints the question, waits for the user to type their name and press Enter, then greets them. The read command takes whatever the user types and stores it in the variable name.
You can make the prompt cleaner by using the -p flag, which combines the prompt message and input on the same line:
#!/bin/bash
read -p "Enter your name: " name
echo "Hello, $name!"This displays Enter your name: and waits for input on the same line, which looks more polished. The -p flag takes the prompt text as its argument, followed by the variable name to store the input.
User input makes scripts interactive and adaptable. Instead of hardcoding values, you can write one script that works differently based on what the user provides each time they run it.
Challenge
EasyCreate a shell script called greeter.sh that asks the user for their favorite color and then displays a message using their input.
Your script should:
- Start with the shebang line (
#!/bin/bash) - Use
readwith the-pflag to prompt:What is your favorite color? - Store the input in a variable called
color - Use
echoto print:Nice! [color] is a great color!(where [color] is the user's input)
Make the script executable and run it. When prompted, enter blue as your answer.
Your output should be:
What is your favorite color? blue
Nice! blue is a great color!Hint: Build the script line by line using
echowith redirection. Useread -p "What is your favorite color? " colorto prompt and capture input, thenecho "Nice! $color is a great color!"to display the result.
Cheat sheet
The read command pauses the script and waits for user input, then stores it in a variable:
#!/bin/bash
echo "What is your name?"
read name
echo "Hello, $name!"Use the -p flag to display the prompt and input on the same line:
#!/bin/bash
read -p "Enter your name: " name
echo "Hello, $name!"The -p flag takes the prompt text as its argument, followed by the variable name to store the input.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Directories
Create A DirectoryCopy A DirectoryMove And Rename A DirectoryDelete A DirectoryRecap - Directory Operations7File Content
Head And TailWord CountSort CommandUnique CommandGrep BasicsGrep With FlagsRecap - Text Detective10Log Analyzer Project
Project OverviewViewing The Log File13Shell Scripting Basics
What Is A Shell ScriptCreate And Run A ScriptVariables In ScriptsUser Input In ScriptsIf StatementFor LoopWhile LoopRecap - Number Guesser2Navigation
Print Working DirectoryList FilesChange DirectoryAbsolute vs Relative PathsHome And Root DirectoryRecap - Find Your Way8Redirection
Standard OutputOverwrite To A FileAppend To A FileStandard InputStandard ErrorRecap - Log Builder6Wildcards And Patterns
The Star WildcardThe Question Mark WildcardBracket WildcardsCombining WildcardsRecap - Selective Operations9Piping
What Is A PipeChaining Two CommandsChaining Multiple CommandsPipe With GrepRecap - Data Pipeline