DEV Community

Cover image for Python as terminal commands
petercour
petercour

Posted on

Python as terminal commands

Python is often used when creating scripts. But did you know you can run it straight from the command line?

No, I don't mean running a .py file from the command, but running a program input.

Whats Python?

Python is a programming language.

The code below won't make sense if you don't know the Python programming language. You should be familiar with the idea of for loops.

Python code as command

You can run Python on a single line with command line as input, like this:

python -c "for i in range(1,10): print(i)"
Enter fullscreen mode Exit fullscreen mode

This then outputs the numbers.
Now you can use the alias command to create custom commands that run this python command

alias forloop='python -c "for i in range(1,10): print(i)"'
Enter fullscreen mode Exit fullscreen mode

Then in your terminal

# forloop
Enter fullscreen mode Exit fullscreen mode

You can store these commands in your .bashrc file.

Is this a good idea? from a security point of view probably not.
See Inject code in Python process.

Top comments (0)