Using Python in a Bash ScriptHow to check the status of bash shell script while executing from Python script?Python script run while screen is lockedHow to grab the oldest unread message from `mail` in a script?Unix: Looking to run a script in directories with a certain nameNeed script to kill python process with low CPU usagebash - get pid for a script using the script filenameSystemd: How to start/stop Python script that should run in background, inside VirtualenvHow to write bash script while using command as condition in if statement?Script using user input to calculate old dateBash scripting for a lab

Scam? Checks via Email

Why would an invisible personal shield be necessary?

Dynamic Icon loading in LWC doesn't work

May a hotel provide accommodation for fewer people than booked?

Does Ubuntu reduce battery life?

Should I put my name first, or last in the team members list

How do I make my photos have more impact?

On the sensitivity conjecture?

Find all the numbers in one file that are not in another file in python

Why did Windows 95 crash the whole system but newer Windows only crashed programs?

When to sell a coin collection

Why would anyone ever invest in a cash-only etf?

If the Moon were impacted by a suitably sized meteor, how long would it take to impact the Earth?

Is it okay for me to decline a project on ethical grounds?

When does the Homunculus die, exactly?

Little Lost Robot

My employer is refusing to give me the pay that was advertised after an internal job move

What language is Raven using for her attack in the new 52?

when to use "wait" and when "busy" mouse cursor

Should students have access to past exams or an exam bank?

Should I intervene when a colleague in a different department makes students run laps as part of their grade?

Why does one get the wrong value when printing counters together?

Is it possible for a particle to decay via gravity?

Is Ear Protection Necessary For General Aviation Airplanes?



Using Python in a Bash Script


How to check the status of bash shell script while executing from Python script?Python script run while screen is lockedHow to grab the oldest unread message from `mail` in a script?Unix: Looking to run a script in directories with a certain nameNeed script to kill python process with low CPU usagebash - get pid for a script using the script filenameSystemd: How to start/stop Python script that should run in background, inside VirtualenvHow to write bash script while using command as condition in if statement?Script using user input to calculate old dateBash scripting for a lab






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















If I try to start python in a bash script, the script will stop running and no commands will execute after "Python" is called. In this simple example, "TESTPRINT" will not be printed. It seems like the script just stops.



#!/bin/bash

python

print("TESTPRINT")

Echo


How do I make the script continue running after going into Python? I believe I had the same problem a few years ago after writing a script that first needed to shell into an Android Phone. Cant remember how I fixed it that time.










share|improve this question







New contributor



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
















  • 1





    This is not how it works. If you want to execute a Python script you'll have to either 1) make a different file and run it with python myfile.py or 2) run an inline script with python -c "script here". You cannot "switch to python" like that.

    – Marco Bonelli
    8 hours ago

















1















If I try to start python in a bash script, the script will stop running and no commands will execute after "Python" is called. In this simple example, "TESTPRINT" will not be printed. It seems like the script just stops.



#!/bin/bash

python

print("TESTPRINT")

Echo


How do I make the script continue running after going into Python? I believe I had the same problem a few years ago after writing a script that first needed to shell into an Android Phone. Cant remember how I fixed it that time.










share|improve this question







New contributor



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
















  • 1





    This is not how it works. If you want to execute a Python script you'll have to either 1) make a different file and run it with python myfile.py or 2) run an inline script with python -c "script here". You cannot "switch to python" like that.

    – Marco Bonelli
    8 hours ago













1












1








1


0






If I try to start python in a bash script, the script will stop running and no commands will execute after "Python" is called. In this simple example, "TESTPRINT" will not be printed. It seems like the script just stops.



#!/bin/bash

python

print("TESTPRINT")

Echo


How do I make the script continue running after going into Python? I believe I had the same problem a few years ago after writing a script that first needed to shell into an Android Phone. Cant remember how I fixed it that time.










share|improve this question







New contributor



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











If I try to start python in a bash script, the script will stop running and no commands will execute after "Python" is called. In this simple example, "TESTPRINT" will not be printed. It seems like the script just stops.



#!/bin/bash

python

print("TESTPRINT")

Echo


How do I make the script continue running after going into Python? I believe I had the same problem a few years ago after writing a script that first needed to shell into an Android Phone. Cant remember how I fixed it that time.







bash shell scripting python






share|improve this question







New contributor



user364877 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



user364877 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






New contributor



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








asked 8 hours ago









user364877user364877

111 bronze badge




111 bronze badge




New contributor



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




New contributor




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












  • 1





    This is not how it works. If you want to execute a Python script you'll have to either 1) make a different file and run it with python myfile.py or 2) run an inline script with python -c "script here". You cannot "switch to python" like that.

    – Marco Bonelli
    8 hours ago












  • 1





    This is not how it works. If you want to execute a Python script you'll have to either 1) make a different file and run it with python myfile.py or 2) run an inline script with python -c "script here". You cannot "switch to python" like that.

    – Marco Bonelli
    8 hours ago







1




1





This is not how it works. If you want to execute a Python script you'll have to either 1) make a different file and run it with python myfile.py or 2) run an inline script with python -c "script here". You cannot "switch to python" like that.

– Marco Bonelli
8 hours ago





This is not how it works. If you want to execute a Python script you'll have to either 1) make a different file and run it with python myfile.py or 2) run an inline script with python -c "script here". You cannot "switch to python" like that.

– Marco Bonelli
8 hours ago










1 Answer
1






active

oldest

votes


















6














To run a set of Python commands from a bash script, you must give the Python interpreter the commands to run, either from a file (Python script), as in



#!/bin/bash

# Create script as "script.py"
cat >script.py <<'END_SCRIPT'
print("TESTPRINT")
END_SCRIPT

# Run script.py
python script.py


... or directly via some form of redirection, for example a here-document:



#!/bin/bash

python - <<'END_SCRIPT'
print("TESTPRINT")
END_SCRIPT


What this does is running python - which instructs the Python interpreter to read the script from standard input. The shell then sends the text of the Python script (delimited by END_SCRIPT in the shell script) to the Python process' standard input stream.



Note that the two bits of code above are subtly different in that the second script's Python process has its standard input connected to the script that it's reading, while the first script's Python process is free to read data other than the script from standard input. This matters if your Python code reads from standard input.



Python can also take a set of commands from the command line directly with its -c option:



#!/bin/bash

python -c 'print("TESTPRINT")'


What you can't do is to "switch to Python" in the middle of a bash script.



The commands in a script is executed by bash one after the other, so your original script would start Python in interactive mode, temporarily suspending the execution of the bash script until the Python process terminates. The script would then try to execute print("TESTPRINT") as a shell command.



It's a similar issue with using ssh like this in a script:



ssh user@server
cd /tmp
ls


(which may possibly be similar to what you say you tried a few years ago).



This would not connect to the remote system and run the cd and ls commands there. It would start an interactive shell on the remote system, and once that shell has terminated (giving control back to the script), cd and ls would be run locally.



Instead, to execute the commands on a remote machine, use



ssh user@server "cd /tmp; ls"


(This is a lame example, but you may get the point).






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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    user364877 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%2f533156%2fusing-python-in-a-bash-script%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    To run a set of Python commands from a bash script, you must give the Python interpreter the commands to run, either from a file (Python script), as in



    #!/bin/bash

    # Create script as "script.py"
    cat >script.py <<'END_SCRIPT'
    print("TESTPRINT")
    END_SCRIPT

    # Run script.py
    python script.py


    ... or directly via some form of redirection, for example a here-document:



    #!/bin/bash

    python - <<'END_SCRIPT'
    print("TESTPRINT")
    END_SCRIPT


    What this does is running python - which instructs the Python interpreter to read the script from standard input. The shell then sends the text of the Python script (delimited by END_SCRIPT in the shell script) to the Python process' standard input stream.



    Note that the two bits of code above are subtly different in that the second script's Python process has its standard input connected to the script that it's reading, while the first script's Python process is free to read data other than the script from standard input. This matters if your Python code reads from standard input.



    Python can also take a set of commands from the command line directly with its -c option:



    #!/bin/bash

    python -c 'print("TESTPRINT")'


    What you can't do is to "switch to Python" in the middle of a bash script.



    The commands in a script is executed by bash one after the other, so your original script would start Python in interactive mode, temporarily suspending the execution of the bash script until the Python process terminates. The script would then try to execute print("TESTPRINT") as a shell command.



    It's a similar issue with using ssh like this in a script:



    ssh user@server
    cd /tmp
    ls


    (which may possibly be similar to what you say you tried a few years ago).



    This would not connect to the remote system and run the cd and ls commands there. It would start an interactive shell on the remote system, and once that shell has terminated (giving control back to the script), cd and ls would be run locally.



    Instead, to execute the commands on a remote machine, use



    ssh user@server "cd /tmp; ls"


    (This is a lame example, but you may get the point).






    share|improve this answer































      6














      To run a set of Python commands from a bash script, you must give the Python interpreter the commands to run, either from a file (Python script), as in



      #!/bin/bash

      # Create script as "script.py"
      cat >script.py <<'END_SCRIPT'
      print("TESTPRINT")
      END_SCRIPT

      # Run script.py
      python script.py


      ... or directly via some form of redirection, for example a here-document:



      #!/bin/bash

      python - <<'END_SCRIPT'
      print("TESTPRINT")
      END_SCRIPT


      What this does is running python - which instructs the Python interpreter to read the script from standard input. The shell then sends the text of the Python script (delimited by END_SCRIPT in the shell script) to the Python process' standard input stream.



      Note that the two bits of code above are subtly different in that the second script's Python process has its standard input connected to the script that it's reading, while the first script's Python process is free to read data other than the script from standard input. This matters if your Python code reads from standard input.



      Python can also take a set of commands from the command line directly with its -c option:



      #!/bin/bash

      python -c 'print("TESTPRINT")'


      What you can't do is to "switch to Python" in the middle of a bash script.



      The commands in a script is executed by bash one after the other, so your original script would start Python in interactive mode, temporarily suspending the execution of the bash script until the Python process terminates. The script would then try to execute print("TESTPRINT") as a shell command.



      It's a similar issue with using ssh like this in a script:



      ssh user@server
      cd /tmp
      ls


      (which may possibly be similar to what you say you tried a few years ago).



      This would not connect to the remote system and run the cd and ls commands there. It would start an interactive shell on the remote system, and once that shell has terminated (giving control back to the script), cd and ls would be run locally.



      Instead, to execute the commands on a remote machine, use



      ssh user@server "cd /tmp; ls"


      (This is a lame example, but you may get the point).






      share|improve this answer





























        6












        6








        6







        To run a set of Python commands from a bash script, you must give the Python interpreter the commands to run, either from a file (Python script), as in



        #!/bin/bash

        # Create script as "script.py"
        cat >script.py <<'END_SCRIPT'
        print("TESTPRINT")
        END_SCRIPT

        # Run script.py
        python script.py


        ... or directly via some form of redirection, for example a here-document:



        #!/bin/bash

        python - <<'END_SCRIPT'
        print("TESTPRINT")
        END_SCRIPT


        What this does is running python - which instructs the Python interpreter to read the script from standard input. The shell then sends the text of the Python script (delimited by END_SCRIPT in the shell script) to the Python process' standard input stream.



        Note that the two bits of code above are subtly different in that the second script's Python process has its standard input connected to the script that it's reading, while the first script's Python process is free to read data other than the script from standard input. This matters if your Python code reads from standard input.



        Python can also take a set of commands from the command line directly with its -c option:



        #!/bin/bash

        python -c 'print("TESTPRINT")'


        What you can't do is to "switch to Python" in the middle of a bash script.



        The commands in a script is executed by bash one after the other, so your original script would start Python in interactive mode, temporarily suspending the execution of the bash script until the Python process terminates. The script would then try to execute print("TESTPRINT") as a shell command.



        It's a similar issue with using ssh like this in a script:



        ssh user@server
        cd /tmp
        ls


        (which may possibly be similar to what you say you tried a few years ago).



        This would not connect to the remote system and run the cd and ls commands there. It would start an interactive shell on the remote system, and once that shell has terminated (giving control back to the script), cd and ls would be run locally.



        Instead, to execute the commands on a remote machine, use



        ssh user@server "cd /tmp; ls"


        (This is a lame example, but you may get the point).






        share|improve this answer















        To run a set of Python commands from a bash script, you must give the Python interpreter the commands to run, either from a file (Python script), as in



        #!/bin/bash

        # Create script as "script.py"
        cat >script.py <<'END_SCRIPT'
        print("TESTPRINT")
        END_SCRIPT

        # Run script.py
        python script.py


        ... or directly via some form of redirection, for example a here-document:



        #!/bin/bash

        python - <<'END_SCRIPT'
        print("TESTPRINT")
        END_SCRIPT


        What this does is running python - which instructs the Python interpreter to read the script from standard input. The shell then sends the text of the Python script (delimited by END_SCRIPT in the shell script) to the Python process' standard input stream.



        Note that the two bits of code above are subtly different in that the second script's Python process has its standard input connected to the script that it's reading, while the first script's Python process is free to read data other than the script from standard input. This matters if your Python code reads from standard input.



        Python can also take a set of commands from the command line directly with its -c option:



        #!/bin/bash

        python -c 'print("TESTPRINT")'


        What you can't do is to "switch to Python" in the middle of a bash script.



        The commands in a script is executed by bash one after the other, so your original script would start Python in interactive mode, temporarily suspending the execution of the bash script until the Python process terminates. The script would then try to execute print("TESTPRINT") as a shell command.



        It's a similar issue with using ssh like this in a script:



        ssh user@server
        cd /tmp
        ls


        (which may possibly be similar to what you say you tried a few years ago).



        This would not connect to the remote system and run the cd and ls commands there. It would start an interactive shell on the remote system, and once that shell has terminated (giving control back to the script), cd and ls would be run locally.



        Instead, to execute the commands on a remote machine, use



        ssh user@server "cd /tmp; ls"


        (This is a lame example, but you may get the point).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 7 hours ago

























        answered 8 hours ago









        KusalanandaKusalananda

        157k18 gold badges311 silver badges496 bronze badges




        157k18 gold badges311 silver badges496 bronze badges























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









            draft saved

            draft discarded


















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












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











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














            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f533156%2fusing-python-in-a-bash-script%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            In Tikz, how to set a node's label alignment to the left?Rotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?Numerical conditional within tikz keys?TikZ/ERD: node (=Entity) label on the insideLine up nested tikz enviroments or how to get rid of themVertically align a tikzpicture and forestDrawing tikz line in the margin for multiple pagesLongtable, contained tikz, padding, custom columns, and an alignment issueTikZ: define arrow starting position based on style and format node labelAlign node name in Tikz