Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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

How to do a grep in a variable? I have stored the wget output in a variable and I need to extract out some strings from it. Like the content of the variable is

upgrade 3.0.5 en_US 4.3 4.1.2

I need to check if the string contains the word upgrade, so I can do a simple grep and then check the exit status of it by $? and proceed.

How can I get the value 3.0.5 which is actually the fourth word?

And how to actually grep in a variable?

share|improve this question
up vote 2 down vote accepted

If you are just looking for a word you can use a for loop.

STRING="upgrade this if you can"
for x in $STRING; do
   echo $x
   if [ "$x" = 'upgrade' ]; then
       echo found
echo $y

If upgrade is always in the same position you could try array assignment.

declare -a z
echo ${z[0]}
share|improve this answer
Thanks a lot :) Although this fits the current question pretty well but Oli's answer is more detailed in case its not as easy as to grab words from a string – Ashfame Feb 11 '11 at 20:56

Rather than grepping, perhaps just use cut. It'll be a lot faster than running a full regex.

STRING="upgrade 3.0.5 en_US 4.3 4.1.2"
echo "$STRING" | cut -d" " -f4

Outputs: 3.0.5

This might be a little more robust though (is grabs the version from the zip url):

echo "$STRING" | sed -n -e 's/.*wordpress-\(.*\)\.zip.*/\1/p'

If you want to store it back in a bash variable, just wrap that in quotes and backticks. The backticked part will execute first, leaving it as as string.

RESULT="`echo "$STRING" | sed -n -e 's/.*wordpress-\(.*\)\.zip.*/\1/p'`"

So as far as stripting this (per your comment) to look for upgrade at the beginning:

STRING="upgrade 3.0.5 en_US 4.3 4.1.2"

if [[ $STRING == upgrade* ]]; then
    VERSION="`echo "$STRING" | sed -n -e 's/.*wordpress-\(.*\)\.zip.*/\1/p'`"
    # do something else now
    # do something if this isn't an upgrade (optional)
share|improve this answer
Cool! That seems to get the fourth word. But I want that to be stored in a variable too. How should I wrap the command to be saved in a variable? Also I need to do a grep too to check the existence of word upgrade or you meant to say that I can first cut the first word out and then check it with the word upgrade and this way it will be faster too? I need to run this every 10mins at the fastest so I would like it to be as much speedy as I can make it. :) – Ashfame Feb 10 '11 at 15:03
@Ashfame Edited. – Oli Feb 10 '11 at 15:13
Ubercool! :D I would need to study about sed first and then I will get back to you. Thanks :) – Ashfame Feb 10 '11 at 15:23 – djeikyb Feb 11 '11 at 10:47
Thanks a lot :) It will be more useful for people who need to catch things in an arbitrary string. I wish I could mark two solutions as answers. – Ashfame Feb 11 '11 at 20:57

Additionally to the overall question you should be aware, that you can also use grep if the string contains several lines and you want to find out the line that contains a certain word.


STRING="Some lines
with the word one
and the word two"

use grep to find the line with the word "one" use echo with the variable quoted:

echo "$STRING" | grep one
share|improve this answer

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.