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

て烟熏妆下的殇ゞ 提交于 2019-11-28 08:07:17

问题


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


回答1:


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




回答2:


Use the alias command:

alias p="python"

You'll probably want to add this to your ~/.bashrc.




回答3:


You can add an alias to your ~/.profile file:

alias p="python"

Note that you can also make a Python script executable with chmod +x script.py. You can then execute it using:

./script.py

You will need to add the following line to the top of your Python code for this to work:

#!/usr/bin/env python

This is called shebang.



来源:https://stackoverflow.com/questions/10986155/how-do-i-alias-a-command-line-command-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!