DEV Community

Cover image for Python as terminal commands
petercour
petercour

Posted on

1 1

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay