I thought it would be nice to have an alias (in ~/.zshrc) to have 'python' aliased to 'ipython' ONLY when 'python' is run without args (otherwise, programs won't launch). First of all, how to express "without args" in an alias? Secondly, what do you think about it?

  • Be more specific as to what you want to accomplish.
    – schaiba
    Feb 16, 2013 at 14:44
  • As of my ipython version (4.0.1), running ipython with a script as the first argument will run the script. Further, note that setting up an alias like this will prevent running, for example, "ipython notebook", so it may be easier to just alias python='ipython' (although I'm sure there will be some undesired side effects occasionally)
    – Achal Dave
    Feb 22, 2016 at 21:39

2 Answers 2


Shell aliases, by definition, don't give you any way to use them conditionally. They apply to anything you run from an interactive prompt.

The way you can do this is with a function instead. (See In Bash, when to alias, when to script, and when to write a function? for more on why this is different.)

function python () {
    test -z "$1" && ipython || command python "$@"

Something along those lines in your shell's rc file will cause your shell to run this function instead of the binary directly. If the first argument is blank, it will fire off ipython for you, otherwise it will pass on all arguments to whatever python binary is in your path (note the use of command to force the binary rather than the function to execute and cause a recursion on itself).

  • This will also execute on python '', and you will also execute python if ipython returns an exit code >0. function also makes this non-POSIX when it doesn't really need to be.
    – Chris Down
    Feb 16, 2013 at 15:38
  • @ChrisDown All of those "side effects" are intentional here. If a blank argument is specified, ipython should be able to handle it. If ipython fails for some reason (such as not existing on the system), trying regular python seemed like a good option. Using function is just a ZSH convention, but your right it would work without that.
    – Caleb
    Feb 16, 2013 at 15:46
  • That isn't an alias, it's a shell function. At least in bash, aliases just replace the command name by a string of fixed words; no arguments as such handled, no logic.
    – vonbrand
    Feb 16, 2013 at 16:54
  • 1
    @vonbrand I didn't say it was an alias. In fact I said that an alias would not work for this problem and that a shell function was needed instead.
    – Caleb
    Feb 16, 2013 at 18:34

Not sure if you can do it in an zsh alias, but why not stick this small shell script in your ~/bin as python:

if test -z "$1"; then
    exec /usr/bin/ipython
    exec /usr/bin/python "$@"
  • 2
    This will also execute on python ''.
    – Chris Down
    Feb 16, 2013 at 15:37
  • As if you'd actually do that :) Feb 16, 2013 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.