Categories
Python Answers

How to run multi-line statements in the one-line command-line with Python?

Spread the love

Sometimes, we want to run multi-line statements in the one-line command-line with Python.

In this article, we’ll look at how to run multi-line statements in the one-line command-line with Python.

How to run multi-line statements in the one-line command-line with Python?

To run multi-line statements in the one-line command-line with Python, we can use pipe.

For instance, we write

echo -e "import sys\nfor r in range(10): print('rob')" | python

to add code into the double quotes with the \n character to add new lines between lines.

And then we echo the string with echo -e to backslash escape the string and pipe the result to python to run it.

Conclusion

To run multi-line statements in the one-line command-line with Python, we can use pipe.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *