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

I initialize variable like this:

dmesgDevs=$(dmesg | grep idVendor)

and want to print it line by line and then check if theres is word in particular line.

share|improve this question

Keeping more lines in a variable can be really painful. In fact when you use only dmesg | grep idVendor the output will be arranged in more lines, but when you do dmesgDevs=$(dmesg | grep idVendor), all the lines will be merged in a single string.

Now, to print dmesgDevs line by line, you can set the internal field separator (IFS) variable, and then let it parse into an array. Something like this it will parse each time one line of input separated by [:

while IFS='[' read ADDR; do for i in "${ADDR[@]}"; do echo $i; done; done <<< "$dmesgDevs"

Finally, to check if theres is word in particular line, just pipe the output of previous command to grep:

while IFS='[' read ADDR; do for i in "${ADDR[@]}"; do echo $i; done; done <<< "$dmesgDevs" | grep 'word'

In this case I was able to find a solution because each line start with the same pattern ([), but this doesn't happen all the time. So, as alternative instead to keep the output of dmesg | grep idVendor command in a variable, keep it in a file; for example in /tmp/dmesgDevs. You can do this using:

dmesg | grep idVendor > /tmp/dmesgDevs

Then, everything is very simple:

cat /tmp/dmesgDevs
grep 'word' /tmp/dmesgDevs
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.