Tyler Filko
Tyler Filko

Reputation: 98

I am trying to capture the output of the bash command

I am trying to capture the output of the bash command PYVER="$(python --version)" within the variable PYVER. For some reason it doesn't seem to work as when I check using echo "$PYVER" it returns nothing (blank line). Through my leaning of bash scripts (I am new to this) I have tried capturing other outputs in this format and it has worked. If anyone can find my error I would be greatly appreciative.

Upvotes: 0

Views: 90

Answers (2)

Ben Castan
Ben Castan

Reputation: 126

The problem I think is that python --version sends it's output to stder, not stdout. The redirects below seem to do what you want.

$PYVER="$(python --version 2>&1 > /dev/null)"
$echo $PYVER
$Python 2.7.10

Upvotes: 1

b__r
b__r

Reputation: 110

since python writes the version to stderr this should work:

PYVER=$(python --version 2>&1)
echo $PYVER

Upvotes: 3

Related Questions