Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am writing a script. What is the difference between the following two lines?

grep . || echo something

and

grep "^\." || echo something
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Grep uses regular expressions; there, a single full stop . matches any single character. To match a literal full stop/period, you need to escape it with a backwards-slash like so: \.. ^ means 'the beginning of the line'.

So, grep . will match anything that contains a single character (so I suppose it wouldn't match empty lines). On the other hand, grep "^\." matches any line that starts with a literal .

In bash, || means 'or'; grep "^\." || echo something will echo something if the grep doesn't turn up any lines (so it evaluates to false). && is used for 'and' in bash.

share|improve this answer
    
Here's a quick start tutorial for better understanding of what's going on. –  gronostaj Jun 16 '13 at 18:24
    
Thank you very much, you are amazing, I don't know where I could find my answer but here :) Thank you –  user231620 Jun 16 '13 at 18:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.