How do I alias a command line command? (Mac)
Asked Answered
L

4

6

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

Loony answered 11/6, 2012 at 19:15 Comment(0)
W
15

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

Westsouthwest answered 11/6, 2012 at 19:16 Comment(6)
this is the best way, in my opinion. however, are you sure the precise path on a mac will be /usr/bin/python?Clements
The more portable approach is to use #!/usr/bin/env python instead of /usr/bin/python.Granddaddy
@SimeonVisser I was just typing that :-)Westsouthwest
@SimeonVisser I've seen posts that have advised against it (though I use it myself) since it may pick up a different version of Python (if there are more than one installed)Westsouthwest
Thank you! I never put the #!/usr/bin/python comment because I never knew what I did. Thanks! I assume if I have a shell script I can do something like #!/usr/bin/bash and achieve the same effect but with shell? Thanks!Loony
@PlazmotechBinary That's exactly right, though of course it depends on where the command is located. For instance, my default shell is the tcsh, but my shell scripts are all bash scripts, so I have #!/bin/bash at the top of the scripts. To locate a command (it'll give you a series of directories, you usually want the first) use the whereis command. E.g., whereis bash or whereis pythonWestsouthwest
A
5

Use the alias command:

alias p="python"

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

Apex answered 11/6, 2012 at 19:17 Comment(0)
G
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.

Granddaddy answered 11/6, 2012 at 19:16 Comment(3)
Note that the details of the alias command depend on whether you are using the bash shell or the tcsh shell (or another one).Sexuality
I typically use #!/usr/bin/env python which typically finds python even if it's in a weird place.Rhadamanthus
@mgilson: I've just updated the post to include this more portable shebang, it's indeed better to use that.Granddaddy
S
1

You can add aliases in the ~/.zshrc file:

alias gs="git status"
alias gc="git commit -m"
alias ga="git add"

alias p="python"

Then close and reopen the terminal to apply the changes.

Sevier answered 6/12, 2022 at 5:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.