Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Scripting General Questions (conditionals and variable passing)

Tags:

bash

macos

I'm rather new to bash scripting, and Google isn't as useful as I'd like for this. I'm just playing around with a little password entry program in my .bash_profile and have something like this:

read PASSWORD
if $PASSWORD != 'pass'; then
    echo "wrong. exiting"
    exit
fi

Unfortunately, this doesn't work. I get these errors (darwin on 10.6)...

EDIT Sorry about this posting. My browser crashed and I didn't even realize this posted. I ended up figuring it out on my own – again sorry. But thanks for the answers!

like image 928
Isaac Avatar asked Sep 14 '26 12:09

Isaac


1 Answers

You are missing square brackets. The if line should be:

if [ $PASSWORD != 'pass' ]; then

or even better:

if [ "$PASSWORD" != 'pass' ]; then

Which will avoid failure if $PASSWORD is empty.

like image 160
Aurélien Gâteau Avatar answered Sep 17 '26 06:09

Aurélien Gâteau



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!