Report this

What is the reason for this report?

How to check if a variable is empty in a bash script?

Posted on November 24, 2020

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!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.