I have my shell script, myscript.sh
below
#!/bin/sh
if [ $1 = "-r" ]; then
echo "I am here"
fi
If I run with . myscript.sh -r
, it works well with message I am here
.
But if I just run with . myscript.sh
, it complaints
-bash: [: =: unary operator expected
What's missing in my script?
You would need to add quotes around $1.
if [ "$1" = "-r" ]; then
echo "I am here"
fi
When $1 is empty you are getting if [ = "-r"] which is a syntax error.
You have missed the quotes:
if [ "$1" = "-r" ]; then
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With