0

Good afternoon everyone,

I have the following problem with BASH:

If I run the following code:

var_echo="echo "
var_text="uno dos tres"
var_pipe=" | awk '{print \$2}' "
EJECUTAR=$($var_echo $var_text $var_pipe )
echo "$EJECUTAR"

#Result:

uno dos tres | awk '{print $2}'

It only shows me the characters stored in the variables without the "echo", but if I execute it in the following way it works

var_echo="echo "
var_text="uno dos tres"
EJECUTAR=$($var_echo $var_text | awk '{print $2}' )
echo "$EJECUTAR"

#Result:

dos

How can I make the pipe with awk work for me from the variable, I don't get the error.

From already thank you very much.

Greetings.

I can't understand why it takes the variable and shows it as text characters and doesn't execute the pipe with awk

shellter
  • 36,525
  • 7
  • 83
  • 90
  • 1
    Please take a look at [how to format my code block](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) – Gilles Quénot Apr 25 '23 at 21:45
  • 2
    Put a valid [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) and paste your script at https://shellcheck.net for validation/recommendation. – Jetchisel Apr 25 '23 at 21:45
  • 2
    And like @shelter says usually, good luck – Gilles Quénot Apr 25 '23 at 21:48
  • Hello everyone, I leave the link with the example. thanks for answering. (https://www.jdoodle.com/a/6aEz) – Pablo Olivera Apr 25 '23 at 22:33
  • 5
    It is normally not a good idea to store code inside variables like this. – jhnc Apr 25 '23 at 22:34
  • 4
    The shell parses syntax (pipes, quotes and escapes, etc) *before* expanding variables, so putting any of those in a variable doesn't work. Generally, use variables for storing data, functions for storing executable code. See [BashFAQ #50: "I'm trying to put a command in a variable, but the complex cases always fail!"](http://mywiki.wooledge.org/BashFAQ/050) – Gordon Davisson Apr 25 '23 at 23:39
  • 1
    Regarding [I leave the link with the example](https://stackoverflow.com/questions/76105661/bash-problems-saving-pipe-awk-to-variable-and-then-executing-it-by-calling-the-v#comment134219118_76105661) - Don't provide information in comments, put it all in your question, and don't provide links or images, just plain text. – Ed Morton Apr 26 '23 at 01:08

0 Answers0