0
line=$(grep "# rvm line" ~/.bashrc)

if [ ! -n "$line" ]; then
  echo "found"
else
  echo "not found"
fi

What's wrong with my quotes in the first line?

EDIT: The problem is set -o errexit, which I use in my script. I suppose -n is treated as an error, exiting the following processes. How can I overcome this, keeping the error check? (Alternatives to -n could work too).

2
  • wrong kind of red-hue? Change the shebang to #!/bin/bash -x or use set -vx and examine the output... Commented Sep 29, 2011 at 13:37
  • you are using -n wrongly, means True if string is not empty and then you negate that using ! Commented Sep 29, 2011 at 13:41

2 Answers 2

3

If you don't plan to use $line for anything else than testing if it's empty or not, then you might as well do this:

if grep -q '# rvm line' ~/.bashrc; then
  echo "found"
else
  echo "not found"
fi
Sign up to request clarification or add additional context in comments.

1 Comment

That's perfect. It's shorter and doesn't exit the script when used with set -o errexit
1

Nothing

Nothing is apparently wrong with the quotes? Are you not getting expected result ?

ok,your if condition is wrong - if it was intended to check that something was returned by the grep statement. I will leave that as a clue for yourself to figure out. Goodluck

1 Comment

hmm.. you're right. It's working by itself, but when it's within my script, it just locks.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.