Why is eval not working with command substitution? [on hold]











up vote
1
down vote

favorite












I have this command



$("${DIR}/test.py" "$1")


the output of which is



export SOME_VAR="hello world"
export ANOTHER_VAR="hello"


But eval $("${DIR}/test.py" "$1")



seems to do nothing. What am I doing wrong?



I want to export those variables in my shell script.



Full bash script:



#!/bin/bash     
DIR="$(cd `dirname $0` && pwd)"
eval $("${DIR}/test.py" "$2")
exit $?


Python script (not full script but give a good idea):



#!/usr/local/bin/python3 
print('export var1="{0}"nexport var2="{1}"nexport var3="{2}"nexport var4="{2}"'.format(string1, string2, string3))









share|improve this question









New contributor




masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Jeff Schaller, ilkkachu, sam, RalfFriedl, G-Man yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Jeff Schaller, ilkkachu, sam, RalfFriedl, G-Man

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    It works for me... bash-4.2$ ./x export SOME_VAR="hello world" export ANOTHER_VAR="hello" bash-4.2$ eval $(./x) bash-4.2$ echo $SOME_VAR hello world . Note you shouldn't have the second export in the output, but it won't stop the code working
    – Stephen Harris
    2 days ago










  • In total, i need to export multiple variables. For me it doesn't stop the code from working but it also doesn't export any vars to the environment. I tried using both #!/bin/bash and #!/bin/sh i am on bash3.2
    – masterpiece
    2 days ago








  • 2




    Are you trying to export the variables to the current shell running the script, not just for the script environment?
    – kevlinux
    2 days ago










  • @kevlinux yes, trying to export to current shell so other programs can use these vars.
    – masterpiece
    2 days ago






  • 1




    Welcome to Unix & Linux. Are you trying to export a variable to a parent process from a child process?
    – Fabby
    2 days ago















up vote
1
down vote

favorite












I have this command



$("${DIR}/test.py" "$1")


the output of which is



export SOME_VAR="hello world"
export ANOTHER_VAR="hello"


But eval $("${DIR}/test.py" "$1")



seems to do nothing. What am I doing wrong?



I want to export those variables in my shell script.



Full bash script:



#!/bin/bash     
DIR="$(cd `dirname $0` && pwd)"
eval $("${DIR}/test.py" "$2")
exit $?


Python script (not full script but give a good idea):



#!/usr/local/bin/python3 
print('export var1="{0}"nexport var2="{1}"nexport var3="{2}"nexport var4="{2}"'.format(string1, string2, string3))









share|improve this question









New contributor




masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Jeff Schaller, ilkkachu, sam, RalfFriedl, G-Man yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Jeff Schaller, ilkkachu, sam, RalfFriedl, G-Man

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    It works for me... bash-4.2$ ./x export SOME_VAR="hello world" export ANOTHER_VAR="hello" bash-4.2$ eval $(./x) bash-4.2$ echo $SOME_VAR hello world . Note you shouldn't have the second export in the output, but it won't stop the code working
    – Stephen Harris
    2 days ago










  • In total, i need to export multiple variables. For me it doesn't stop the code from working but it also doesn't export any vars to the environment. I tried using both #!/bin/bash and #!/bin/sh i am on bash3.2
    – masterpiece
    2 days ago








  • 2




    Are you trying to export the variables to the current shell running the script, not just for the script environment?
    – kevlinux
    2 days ago










  • @kevlinux yes, trying to export to current shell so other programs can use these vars.
    – masterpiece
    2 days ago






  • 1




    Welcome to Unix & Linux. Are you trying to export a variable to a parent process from a child process?
    – Fabby
    2 days ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have this command



$("${DIR}/test.py" "$1")


the output of which is



export SOME_VAR="hello world"
export ANOTHER_VAR="hello"


But eval $("${DIR}/test.py" "$1")



seems to do nothing. What am I doing wrong?



I want to export those variables in my shell script.



Full bash script:



#!/bin/bash     
DIR="$(cd `dirname $0` && pwd)"
eval $("${DIR}/test.py" "$2")
exit $?


Python script (not full script but give a good idea):



#!/usr/local/bin/python3 
print('export var1="{0}"nexport var2="{1}"nexport var3="{2}"nexport var4="{2}"'.format(string1, string2, string3))









share|improve this question









New contributor




masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have this command



$("${DIR}/test.py" "$1")


the output of which is



export SOME_VAR="hello world"
export ANOTHER_VAR="hello"


But eval $("${DIR}/test.py" "$1")



seems to do nothing. What am I doing wrong?



I want to export those variables in my shell script.



Full bash script:



#!/bin/bash     
DIR="$(cd `dirname $0` && pwd)"
eval $("${DIR}/test.py" "$2")
exit $?


Python script (not full script but give a good idea):



#!/usr/local/bin/python3 
print('export var1="{0}"nexport var2="{1}"nexport var3="{2}"nexport var4="{2}"'.format(string1, string2, string3))






bash shell-script command-substitution eval






share|improve this question









New contributor




masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









Filipe Brandenburger

6,1141725




6,1141725






New contributor




masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









masterpiece

163




163




New contributor




masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by Jeff Schaller, ilkkachu, sam, RalfFriedl, G-Man yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Jeff Schaller, ilkkachu, sam, RalfFriedl, G-Man

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by Jeff Schaller, ilkkachu, sam, RalfFriedl, G-Man yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Jeff Schaller, ilkkachu, sam, RalfFriedl, G-Man

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    It works for me... bash-4.2$ ./x export SOME_VAR="hello world" export ANOTHER_VAR="hello" bash-4.2$ eval $(./x) bash-4.2$ echo $SOME_VAR hello world . Note you shouldn't have the second export in the output, but it won't stop the code working
    – Stephen Harris
    2 days ago










  • In total, i need to export multiple variables. For me it doesn't stop the code from working but it also doesn't export any vars to the environment. I tried using both #!/bin/bash and #!/bin/sh i am on bash3.2
    – masterpiece
    2 days ago








  • 2




    Are you trying to export the variables to the current shell running the script, not just for the script environment?
    – kevlinux
    2 days ago










  • @kevlinux yes, trying to export to current shell so other programs can use these vars.
    – masterpiece
    2 days ago






  • 1




    Welcome to Unix & Linux. Are you trying to export a variable to a parent process from a child process?
    – Fabby
    2 days ago














  • 1




    It works for me... bash-4.2$ ./x export SOME_VAR="hello world" export ANOTHER_VAR="hello" bash-4.2$ eval $(./x) bash-4.2$ echo $SOME_VAR hello world . Note you shouldn't have the second export in the output, but it won't stop the code working
    – Stephen Harris
    2 days ago










  • In total, i need to export multiple variables. For me it doesn't stop the code from working but it also doesn't export any vars to the environment. I tried using both #!/bin/bash and #!/bin/sh i am on bash3.2
    – masterpiece
    2 days ago








  • 2




    Are you trying to export the variables to the current shell running the script, not just for the script environment?
    – kevlinux
    2 days ago










  • @kevlinux yes, trying to export to current shell so other programs can use these vars.
    – masterpiece
    2 days ago






  • 1




    Welcome to Unix & Linux. Are you trying to export a variable to a parent process from a child process?
    – Fabby
    2 days ago








1




1




It works for me... bash-4.2$ ./x export SOME_VAR="hello world" export ANOTHER_VAR="hello" bash-4.2$ eval $(./x) bash-4.2$ echo $SOME_VAR hello world . Note you shouldn't have the second export in the output, but it won't stop the code working
– Stephen Harris
2 days ago




It works for me... bash-4.2$ ./x export SOME_VAR="hello world" export ANOTHER_VAR="hello" bash-4.2$ eval $(./x) bash-4.2$ echo $SOME_VAR hello world . Note you shouldn't have the second export in the output, but it won't stop the code working
– Stephen Harris
2 days ago












In total, i need to export multiple variables. For me it doesn't stop the code from working but it also doesn't export any vars to the environment. I tried using both #!/bin/bash and #!/bin/sh i am on bash3.2
– masterpiece
2 days ago






In total, i need to export multiple variables. For me it doesn't stop the code from working but it also doesn't export any vars to the environment. I tried using both #!/bin/bash and #!/bin/sh i am on bash3.2
– masterpiece
2 days ago






2




2




Are you trying to export the variables to the current shell running the script, not just for the script environment?
– kevlinux
2 days ago




Are you trying to export the variables to the current shell running the script, not just for the script environment?
– kevlinux
2 days ago












@kevlinux yes, trying to export to current shell so other programs can use these vars.
– masterpiece
2 days ago




@kevlinux yes, trying to export to current shell so other programs can use these vars.
– masterpiece
2 days ago




1




1




Welcome to Unix & Linux. Are you trying to export a variable to a parent process from a child process?
– Fabby
2 days ago




Welcome to Unix & Linux. Are you trying to export a variable to a parent process from a child process?
– Fabby
2 days ago










2 Answers
2






active

oldest

votes

















up vote
2
down vote













The example given works for me as well, but there is one potential source of trouble: depending on the values being defined, you may need double-quotes around the command substitution to prevent word splitting and wildcard expansion:



eval "$("${DIR}/test.py" "$1")"


Note that at the very least, not having double-quotes will cause troubles if the output contains multiple commands separated by newlines. That is because without the double-quotes, the output gets split into "words" based on whitespace (generally spaces, tabs, and newlines), and then eval splices it back together with spaces. Net result: all newlines get converted to spaces, and so all the commands that're supposed to be separate lines get mushed together into a single long mess of a command.



For an extreme example, suppose test.py outputs this:



# Define some variables:
export SOME_VAR="6 * 5"


The split-expand-merge process gives this resulting command:



# Define some variables: export SOME_VAR="6 file1.txt file2.txt otherfile.png 5"


(where "file1.txt file2.txt otherfile.png" are all the files in the current directory.) This isn't what you want at all.






share|improve this answer























  • thx Gordon. I will keep your tip in mind. The eval "$("${DIR}/test.py" "$1")" didnt solve it for me though.
    – masterpiece
    2 days ago










  • posted full script
    – masterpiece
    2 days ago


















up vote
1
down vote













The problem was not using source to allow exporting vars to the current shell.
Thanks everyone for your help.






share|improve this answer








New contributor




masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    The example given works for me as well, but there is one potential source of trouble: depending on the values being defined, you may need double-quotes around the command substitution to prevent word splitting and wildcard expansion:



    eval "$("${DIR}/test.py" "$1")"


    Note that at the very least, not having double-quotes will cause troubles if the output contains multiple commands separated by newlines. That is because without the double-quotes, the output gets split into "words" based on whitespace (generally spaces, tabs, and newlines), and then eval splices it back together with spaces. Net result: all newlines get converted to spaces, and so all the commands that're supposed to be separate lines get mushed together into a single long mess of a command.



    For an extreme example, suppose test.py outputs this:



    # Define some variables:
    export SOME_VAR="6 * 5"


    The split-expand-merge process gives this resulting command:



    # Define some variables: export SOME_VAR="6 file1.txt file2.txt otherfile.png 5"


    (where "file1.txt file2.txt otherfile.png" are all the files in the current directory.) This isn't what you want at all.






    share|improve this answer























    • thx Gordon. I will keep your tip in mind. The eval "$("${DIR}/test.py" "$1")" didnt solve it for me though.
      – masterpiece
      2 days ago










    • posted full script
      – masterpiece
      2 days ago















    up vote
    2
    down vote













    The example given works for me as well, but there is one potential source of trouble: depending on the values being defined, you may need double-quotes around the command substitution to prevent word splitting and wildcard expansion:



    eval "$("${DIR}/test.py" "$1")"


    Note that at the very least, not having double-quotes will cause troubles if the output contains multiple commands separated by newlines. That is because without the double-quotes, the output gets split into "words" based on whitespace (generally spaces, tabs, and newlines), and then eval splices it back together with spaces. Net result: all newlines get converted to spaces, and so all the commands that're supposed to be separate lines get mushed together into a single long mess of a command.



    For an extreme example, suppose test.py outputs this:



    # Define some variables:
    export SOME_VAR="6 * 5"


    The split-expand-merge process gives this resulting command:



    # Define some variables: export SOME_VAR="6 file1.txt file2.txt otherfile.png 5"


    (where "file1.txt file2.txt otherfile.png" are all the files in the current directory.) This isn't what you want at all.






    share|improve this answer























    • thx Gordon. I will keep your tip in mind. The eval "$("${DIR}/test.py" "$1")" didnt solve it for me though.
      – masterpiece
      2 days ago










    • posted full script
      – masterpiece
      2 days ago













    up vote
    2
    down vote










    up vote
    2
    down vote









    The example given works for me as well, but there is one potential source of trouble: depending on the values being defined, you may need double-quotes around the command substitution to prevent word splitting and wildcard expansion:



    eval "$("${DIR}/test.py" "$1")"


    Note that at the very least, not having double-quotes will cause troubles if the output contains multiple commands separated by newlines. That is because without the double-quotes, the output gets split into "words" based on whitespace (generally spaces, tabs, and newlines), and then eval splices it back together with spaces. Net result: all newlines get converted to spaces, and so all the commands that're supposed to be separate lines get mushed together into a single long mess of a command.



    For an extreme example, suppose test.py outputs this:



    # Define some variables:
    export SOME_VAR="6 * 5"


    The split-expand-merge process gives this resulting command:



    # Define some variables: export SOME_VAR="6 file1.txt file2.txt otherfile.png 5"


    (where "file1.txt file2.txt otherfile.png" are all the files in the current directory.) This isn't what you want at all.






    share|improve this answer














    The example given works for me as well, but there is one potential source of trouble: depending on the values being defined, you may need double-quotes around the command substitution to prevent word splitting and wildcard expansion:



    eval "$("${DIR}/test.py" "$1")"


    Note that at the very least, not having double-quotes will cause troubles if the output contains multiple commands separated by newlines. That is because without the double-quotes, the output gets split into "words" based on whitespace (generally spaces, tabs, and newlines), and then eval splices it back together with spaces. Net result: all newlines get converted to spaces, and so all the commands that're supposed to be separate lines get mushed together into a single long mess of a command.



    For an extreme example, suppose test.py outputs this:



    # Define some variables:
    export SOME_VAR="6 * 5"


    The split-expand-merge process gives this resulting command:



    # Define some variables: export SOME_VAR="6 file1.txt file2.txt otherfile.png 5"


    (where "file1.txt file2.txt otherfile.png" are all the files in the current directory.) This isn't what you want at all.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 2 days ago

























    answered 2 days ago









    Gordon Davisson

    1,18655




    1,18655












    • thx Gordon. I will keep your tip in mind. The eval "$("${DIR}/test.py" "$1")" didnt solve it for me though.
      – masterpiece
      2 days ago










    • posted full script
      – masterpiece
      2 days ago


















    • thx Gordon. I will keep your tip in mind. The eval "$("${DIR}/test.py" "$1")" didnt solve it for me though.
      – masterpiece
      2 days ago










    • posted full script
      – masterpiece
      2 days ago
















    thx Gordon. I will keep your tip in mind. The eval "$("${DIR}/test.py" "$1")" didnt solve it for me though.
    – masterpiece
    2 days ago




    thx Gordon. I will keep your tip in mind. The eval "$("${DIR}/test.py" "$1")" didnt solve it for me though.
    – masterpiece
    2 days ago












    posted full script
    – masterpiece
    2 days ago




    posted full script
    – masterpiece
    2 days ago












    up vote
    1
    down vote













    The problem was not using source to allow exporting vars to the current shell.
    Thanks everyone for your help.






    share|improve this answer








    New contributor




    masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote













      The problem was not using source to allow exporting vars to the current shell.
      Thanks everyone for your help.






      share|improve this answer








      New contributor




      masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        1
        down vote










        up vote
        1
        down vote









        The problem was not using source to allow exporting vars to the current shell.
        Thanks everyone for your help.






        share|improve this answer








        New contributor




        masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        The problem was not using source to allow exporting vars to the current shell.
        Thanks everyone for your help.







        share|improve this answer








        New contributor




        masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 2 days ago









        masterpiece

        163




        163




        New contributor




        masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        masterpiece is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.















            Popular posts from this blog

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt