0

I want to execute a string command and then get the result of it to assign a variable in shell script bash. for example :

strCode="scontrol show jodid --dd $VALUE"
eval $strCode

The point here is this codepart worked but how can I get the results from the `

eval

` command and assign it to a variable, because I need it to use.

when I type the theResult=eval $strCode it does not work.

thanks for the help

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • Storing commands in variables is generally a bad idea; see [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050) (and many previous questions here). Variables are for data, not executable code (and `eval` is a massive bug magnet). Storing *the result of running a command*, on the other hand, is a perfectly reasonable thing to use a variable for. – Gordon Davisson Mar 08 '22 at 08:55
  • See _Command substitution_ in the bash man page. – user1934428 Mar 08 '22 at 09:11
  • problem SOLVED, thanks to all of you.. – Bilal KOÇAK Mar 08 '22 at 09:21

2 Answers2

0

The solution is:

strCode=$(scontrol show jodid --dd $VALUE)
echo "${strCode}"
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
0

It's easier than you are doing:

theResult=$(scontrol show jodid --dd $VALUE)

You should follow some bash tutorial, as this is a quite basic question.

Poshi
  • 5,332
  • 3
  • 15
  • 32