how to use eval in linux command if string conatins special character such as – [closed]

I have tried this command

eval('my-value')

but this error

File "<string>", line 1, in <module>
NameError: name 'my' is not defined

but if i try

eval('myval')

i get this

  File "<string>", line 1, in <module>
NameError: name 'myval' is not defined

why did it is reading my and value differently in first command? how to tackle if a varible name conatins –

  • this looks like javascript, not a shell language

    – 

  • since this is javascript you are doing subtraction between the values my and value

    – 

  • no i am trying to exeute this command python -c “import my_module; my_module.my_function(eval(‘var1’), eval(‘var2’))” and the value of var1 is my-value @DanielA.White

    – 




  • The Linux tag should only be used for questions specific to Linux, meaning problems you couldn’t cause outside Linux if you tried. Similarly, the command-line tag doesn’t make sense here — you’d have the same problem running the same code in a way that involved no command lines at all.

    – 




  • Clarify your question by editing it; things that are only in comments don’t count as part of the question for purposes of being fair game to use in writing answers (so by a strict interpretation of the rules, @NapoleanDynamite shouldn’t have edited to take your above comment into account until you updated your question to incorporate the content of that comment).

    – 




(EDITED)
this clearly is not a linux command? It seems that you are in python environment rather than a shell.

If thats the case you should probably do something similar to this:

your_value = eval('myval')

OK, given your new context, you could try to use double quotes around the entire command and single quotes around the string arguments inside the eval, like:

python -c "import my_module; my_module.my_function(eval('\"$var1\"'), eval('\"$var2\"'))"

change var1 and var2 depending on your needs

Leave a Comment