When is grep searched for patterns to a file with quotes and/or without quotes in bash? [duplicate]

I’m confused over whether grep is searched for patterns include quotes or omit them and when the patterns are treated literally in bash.

My experience so far led me to believe that grep automatically treats the search pattern in quotes as a single string. E.g.:

If file.txt contains:

Welcome to stack*overflow !

The above appears to special character * that can be interpreted as a single string in txt file, but also can be interpreted as a wildcard character by the bash shell before grep processes it.

I understand that quotes can be used to treat the pattern as a single string. If I were to call the bash shell with quotes…

> grep "stack*overflow" file.txt
> 

… isn’t it the case that the bash shell prints the line containing the exact string “stack*overflow”? If not, why does it not produce any results when run this way?

Similarly, why does it also not produce any results when I were to call the bash shell without quotes:

> grep stack*overflow file.txt
> 

What’s the difference between grep behave with quotes and without quotes in bash shell?

1

This is basically a duplicate of When to wrap quotes around a shell variable? but let’s review specifically for regular expressions.

When the shell sees

grep .* file.txt

it will expand the wildcard if it can; so if you have files named .foo and .bar, you will be running

grep .bar .foo file.txt

i.e. looking for (any character followed by) bar in .foo and file.txt, which almost certainly isn’t what you want.

The simplest and most straightforward guideline is to always, forever, without exceptions use quotes around your regular expressions. Single quotes are absolutely the best because they quote verbatim, though sometimes (such as when your actual regular expression needs to contain a single quote) you need to fall back to double quotes, and then understand that any backtick, dollar sign, or backslash needs to be backslash-escaped.

There are corner cases where omitting the quotes is safe, but probably stupid. There are also corner cases around how exactly the shell will expand wildcards (nullglob etc) but if you make sure you always use quoted regular expressions, that’s irrelevant.

Tagentially, you also seem to be confused about the differences between shell wildcard (glob) patterns and regular expressions. Stack* in a regular expression looks for Stac followed by zero or more occurrences of k, as many as possible. See also What are the differences between glob-style patterns and regular expressions? as well as perhaps the Stack Overflow regex tag info page which contains a brief beginner FAQ and links to learning resources.

To search for a literal asterisk, the regular expression is * or [*]. To search for anything (any character, as many as possible) the regular expression is .* (any character, zero or more). In this case, perhaps you want [^A-Za-z0-9]* which looks for as many nonalphanumeric characters as possible.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật