Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading