Search code examples
pythoneval

how to use eval in linux command if string conatins special character such as -


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 -


Solution

  • (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