0

I have made a command witch return 'version:X' .

ie:

$>./mybox -v
$>version:2

I don't understand why this isn't working :

 $>VERSION=$( /home/mybox -v | sed 's/.*version:\([0-9]*\).*/\1/')
 $>echo $VERSION
 $>

if I write this, it is ok :

 $>VERSION=$( echo "version:2" | sed 's/.*version:\([0-9]*\).*/\1/')
 $>echo $VERSION
 $>2

Regards

0

1 Answer 1

5

It's pretty common for version/error/debugging information to be sent to stderr, not stdout. When running the command from a terminal, both will be printed, but only stdout will make it through the pipe to sed.

echo output always goes to stdout by default, which is why you're not having trouble there.

If the above is correct, you'll just need to redirect stderr (file descriptor 2) to stdout (file descriptor 1) before passing it along:

VERSION=$( /home/mybox -v 2>&1 | sed 's/.*version:\([0-9]*\).*/\1/')
#                         ^^^^
Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't "version:2" appear at the console in this case? It doesn't in the example in the question.
@Sleafar Typically yes, unless it was ignored in copy-paste or an artifact of console line-overwriting. This could have been a comment, but it was getting long and I decided to go with the hunch instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.