Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: Can't get simple 'if' to work

Tags:

bash

I don't understand why this simple read doesn't work. Mind you, I am VERY new to bash. :)

#!/bin/bash

echo -n "Project Name: " 
read PROJECT_NAME

if [ -n "$PROJECT_NAME" ]; then
    echo "You must provide a project name."
    exit 2
fi

-- snip --

When this executes, it asks for the project name. After I press enter, I get "You must provide a project name." and then the scripts exists instead of continuing.

What am I doing wrong?

Thanks Eric


1 Answers

You want [ -z "$PROJECT_NAME" ], not -n:

From man test:

   -n STRING
          the length of STRING is nonzero

...

   -z STRING
          the length of STRING is zero
like image 56
geocar Avatar answered Jan 30 '26 11:01

geocar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!