How to have default values with “read” in Bash

I am slowly discovering the powerful Bash. Here's something you'll need eventually when working with Bash scripts and user input.

How do you prompt the user to enter something in a Bash script?

echo "What's your name?" read name

Of course you can print it later with:

echo $name

How do you set a default value?

echo "What's your name? (default: Ricard)" read name name="${name:=Ricard}"

This syntax checks if name is empty. If so, it will set it to Ricard

About Ricard Torres

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*