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)"
python -c "for i in range(1,10): print(i)"
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)"'
alias forloop='python -c "for i in range(1,10): print(i)"'
alias forloop='python -c "for i in range(1,10): print(i)"'

Enter fullscreen mode Exit fullscreen mode

Then in your terminal

# forloop
# forloop
# 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.

原文链接:Python as terminal commands

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
Sometimes a winner is just a dreamer that never gives up.
有时候,成功者只是坚持梦想不放弃的人
评论 抢沙发

请登录后发表评论

    暂无评论内容