1

I'm writing a bash script which runs other commands such as calling for yum to install a list of packages, and I'd like my script to silence the other commands by default, but let them output if I pass the -v argument. Issue I'm running into is that checking the value of $1 doesn't appear to be working correctly. Given the following code, my script will always echo "Yes":

if [[ "$1"=="-v" ]]; then
    echo "Yes"
else
    echo "No"
fi

If I just echo $1 and pass the script the -v, it echos -v as it should. What am I missing here?

EDIT: Found it. Kept playing with the script and changed the first line to this:

if [[ $1 == "-v" ]]; then

Works now?

1
  • 1
    The version without spaces is always true: when a single argument is given inside [[ ]], if that argument is non-empty then [[ returns true. Commented Nov 12, 2012 at 19:06

1 Answer 1

1

You need to add some spaces around your check: if [[ "$1" == "-v" ]]; then

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I could have sworn I tried adding the spaces already and it hadn't worked, but like I mentioned in my edit, it's working with that change. I shoulda gotten that extra hour of sleep last night.
My answer and your edit must have passed each other on submit. Glad you worked it out! Don't forget that you can post an answer you find and accept it.
I'll try to remember that next time. :P Still waiting for it to let me accept yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.