Question
How to check if a variable is empty in a bash script?
I recently got asked how to check if a variable is empty in a Bash script.
Here is an example script, where we expect the user to enter a value when asked to do so:
#!/bin/bash
echo "Enter your name: "
read name
echo "Hello ${name}"
If you run the script, you will be able to press enter, and the output would be just Hello
.
Here’s how to check if a variable is empty in a Bash script!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
×