Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Let's say I'm doing a grep and it returns this line:

Invalid value (48) on line 3

How can I easily pull that value 48 into a variable in Bourne shell?

share|improve this question
With pure "classic" Bourne Shell commands, or can you use external tools? No Bash or anything recent? – slhck Aug 13 '13 at 16:35

If you are sure that the pattern is always to get the value in the first pair of parenthesis, then cut is your best friend.

myvar=$(echo 'Invalid value (48) on line 3' | cut -d\( -f2 | cut -d\) -f1)

this extracts the value between the parens.

share|improve this answer
$() is not part of SVID and thus not strictly "Bourne Shell" only. It's defined in POSIX though. – slhck Aug 13 '13 at 18:13
yes, if it is indeed the original sh, though in most linux distributions /bin/sh is just a symlink to /bin/bash. however, backstick should work, if $() does not. just remove $ and replace () with ``. – johnshen64 Aug 13 '13 at 18:16
Yup, just wanted to mention it for completeness since the OP seemed to be very specific about sh and not Bash and companions. – slhck Aug 13 '13 at 18:18

echo 'Invalid value (48) on line 3'| awk -F'[()]' '{print $2}'

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.