Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to have bash parse/extract a full URL (and only the url) from a random short string.


bob, the address is http://www.google.com


https://foo.com/category/example.html is up


Error 123 occurred at http://bit.ly/~1223456677878


Stats are up: https://foo1234.net/report.jpg

I tried using cat foo_output | egrep -o "https?://[\w'-\.]*\s" but that didn't seem to work.

share|improve this question
Sounds scary, depending on what you want to do with the extracted URL... – vonbrand Mar 4 '14 at 17:28

Did you try:

egrep -o 'https?://[^ ]+' foo_output


Note that anything with a character class is taken as literal, so saying [\w] doesn't match a word character. Moreover, you don't need to escape a regex metacharacter within a character class, i.e, saying [\.] isn't quite the same as [.].

share|improve this answer
Bingo! That works. Thanks. – Mike B Mar 4 '14 at 6:48
[^ ] is too wide, you'll want to exclude other blanks, (, ), possibly comas, and all the characters that are not allowed in URLs. – Stéphane Chazelas Mar 4 '14 at 10:13
@StephaneChazelas You're right. However, I assumed that the URL is preceded and followed by a space unless at the beginning or the end of line. – devnull Mar 4 '14 at 10:18

URIs aren't well-suited for regular expression matching when embedded in natural language. However, the current state of the art is John Gruber's Improved Liberal, Accurate Regex Pattern for Matching URLs. As currently posted, the one-line version is as follows:


John also appears to maintain a gist here, although his blog entry does a much better job of explaining his test corpus and the limitations of the regular expression pattern.

If you want to implement the expression from the command line, you may find yourself limited by the regular expression engine you're using or by shell quoting issues. I've found a Ruby script to be the best option, but your mileage may vary.

share|improve this answer
Please include the regex in your answer instead of linking to it. – terdon Mar 4 '14 at 17:00
@terdon, the full regexp is some 60 lines. – vonbrand Mar 4 '14 at 17:30
@vonbrand I know, I saw it. We just tend to avoid linking to external resources. The whole point of the SE sites is to be a wiki. What if the blog you linked to goes offline? Your answer will become useless. Anyway, 60 lines is not that much and it is only 60 lines for readability. – terdon Mar 4 '14 at 17:32

The problem with matching URLs is that just about anything can be in a URL:


As you can see, the (valid) URL above contains $,?,#,&,,,. and :. Basically, the only thing you can be sure a URL does not contain is a blank space. With that in mind, you could extract your URLs with as simple a pattern as:

$ grep -oP 'http.?://\S+' file 

The \S matches any non-space characters in perl compatible regular expressions (PCREs), the -P activates PCREs for grep and the -o makes it print only the matched segment of the line.

share|improve this answer

Just egrep -o 'https?://[^ ")]+'

which will include url() and "http"

share|improve this answer
How is this different from the answer by devnull? I hope you realise that the use of egrep is deprecated. – Anthon Apr 26 at 20:25
If you have an improvement over an existing answer, you can refer back to via the "share" link under that answer. See also the help pages – Jeff Schaller Apr 26 at 20:48

Your Answer


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.