-1

I am trying to save the specific output from a piped command to a variable.

value= ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r"

This saves the average ping to the variable "value". However when I try to further process the variable e.g. in an echo line like:

echo "The Average ping is: $variable"

The output is

The Average ping is: $variable

Even when i try to pass the value to another Variable like:

value2= $value

the result is the same.

I read that variables in bash need to be declared in a certain way, may this be the problem in this specific case?

Bjeran
  • 15
  • 5
  • 2
    Possible duplicate of [How do I set a variable to the output of a command in Bash?](https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash) – wjandrea Nov 03 '19 at 22:08
  • If that's not a duplicate, you need to [edit] the question and make a [mre], cause those commands don't match what you're describing. – wjandrea Nov 03 '19 at 22:09

1 Answers1

0

sh or bash:

value="`ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r"`"

bash:

value="$(ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r")"