Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
662 views
in Technique[技术] by (71.8m points)

python - How do I alias a command line command? (Mac)

I'm on a mac, and I write quite a bit of python scripts.

Every time I need to run them, I have to type 'python script_name.py'. Is there I way to make it so I only have to type like 'p script_name.py'? It would save some time :D

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am assuming you are running your script from the command line right? If so, add the following line as the first line in your script:

#!/usr/bin/python

or alternatively

#!/usr/bin/env python

in case the python command is not located in /usr/bin, and then issue the following command once at the Unix/terminal prompt (it makes your script "executable"):

chmod +x script_name.py

from then on you only need to type the name of the script at the command prompt to run it. No python part of the command needed. I.e., simply

./script_name.py 

will run the script.

You can also of course go with the alias, but the above is a cleaner solution in my opinion.

For the alias

alias p="python"

should go into your ~/.bashrc file


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...