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 nameCode language: PHP (php)

Of course you can print it later with:

echo $nameCode language: PHP (php)

How do you set a default value?

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

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

Comments

  1. The big dog says:

    worked a treat , thanks ๐Ÿ‘

    1. Glad it helped, Big Dog ๐Ÿ˜

Leave a Reply

Your email address will not be published. Required fields are marked *