Use subprocess in python to echo a variable

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have a variable in python, and I'm trying to open a subprocess and echo the variable, then create a file with the variable in it.
I tried this:



subprocess.Popen(['echo "$var" > file.txt'], shell=True)


It creates the file, but it's empty. How can I get the result that I want?










share|improve this question









New contributor




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















  • 1




    Not sure of your use case, but it might be easier and more robust to open the file, format the variable appropriately into a string, and write the string to the file.
    – Mark Plotnick
    1 hour ago














up vote
0
down vote

favorite












I have a variable in python, and I'm trying to open a subprocess and echo the variable, then create a file with the variable in it.
I tried this:



subprocess.Popen(['echo "$var" > file.txt'], shell=True)


It creates the file, but it's empty. How can I get the result that I want?










share|improve this question









New contributor




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















  • 1




    Not sure of your use case, but it might be easier and more robust to open the file, format the variable appropriately into a string, and write the string to the file.
    – Mark Plotnick
    1 hour ago












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a variable in python, and I'm trying to open a subprocess and echo the variable, then create a file with the variable in it.
I tried this:



subprocess.Popen(['echo "$var" > file.txt'], shell=True)


It creates the file, but it's empty. How can I get the result that I want?










share|improve this question









New contributor




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











I have a variable in python, and I'm trying to open a subprocess and echo the variable, then create a file with the variable in it.
I tried this:



subprocess.Popen(['echo "$var" > file.txt'], shell=True)


It creates the file, but it's empty. How can I get the result that I want?







linux shell-script python echo






share|improve this question









New contributor




david 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




david 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 12 mins ago









mrc02_kr

1,037320




1,037320






New contributor




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









asked 2 hours ago









david

1




1




New contributor




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





New contributor





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






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







  • 1




    Not sure of your use case, but it might be easier and more robust to open the file, format the variable appropriately into a string, and write the string to the file.
    – Mark Plotnick
    1 hour ago












  • 1




    Not sure of your use case, but it might be easier and more robust to open the file, format the variable appropriately into a string, and write the string to the file.
    – Mark Plotnick
    1 hour ago







1




1




Not sure of your use case, but it might be easier and more robust to open the file, format the variable appropriately into a string, and write the string to the file.
– Mark Plotnick
1 hour ago




Not sure of your use case, but it might be easier and more robust to open the file, format the variable appropriately into a string, and write the string to the file.
– Mark Plotnick
1 hour ago










1 Answer
1






active

oldest

votes

















up vote
0
down vote













In Python you don't use $ sign to use a variable. Also when you want to embed variable into string, you cannot just simply use variable name in string. You should do something like that:



subprocess.Popen(['echo "" > file.txt'.format(var)], shell=True)


This is great website which will explain you how to use .format method.






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    david is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476393%2fuse-subprocess-in-python-to-echo-a-variable%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    In Python you don't use $ sign to use a variable. Also when you want to embed variable into string, you cannot just simply use variable name in string. You should do something like that:



    subprocess.Popen(['echo "" > file.txt'.format(var)], shell=True)


    This is great website which will explain you how to use .format method.






    share|improve this answer
























      up vote
      0
      down vote













      In Python you don't use $ sign to use a variable. Also when you want to embed variable into string, you cannot just simply use variable name in string. You should do something like that:



      subprocess.Popen(['echo "" > file.txt'.format(var)], shell=True)


      This is great website which will explain you how to use .format method.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        In Python you don't use $ sign to use a variable. Also when you want to embed variable into string, you cannot just simply use variable name in string. You should do something like that:



        subprocess.Popen(['echo "" > file.txt'.format(var)], shell=True)


        This is great website which will explain you how to use .format method.






        share|improve this answer












        In Python you don't use $ sign to use a variable. Also when you want to embed variable into string, you cannot just simply use variable name in string. You should do something like that:



        subprocess.Popen(['echo "" > file.txt'.format(var)], shell=True)


        This is great website which will explain you how to use .format method.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        mrc02_kr

        1,037320




        1,037320




















            david is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            david is a new contributor. Be nice, and check out our Code of Conduct.












            david is a new contributor. Be nice, and check out our Code of Conduct.











            david is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476393%2fuse-subprocess-in-python-to-echo-a-variable%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay