Creating an alias for Python in bash/zsh

aliasbashpythonterminalzsh

I have often seen people recommending to setup an alias in one's .bash_profile or .zshrc along the lines of

alias python='python3'

This seems like a convenient idea, however, as far as I understand, python2 is still installed for compatibility reasons, so won't creating such an alias break certain things?

Best Answer

From man bash

Aliases  are not expanded when the shell is not interactive, unless the
expand_aliases shell option is set using shopt (see the description  of
shopt under SHELL BUILTIN COMMANDS below).

So unless you explicitly set an option, aliases aren't expanded in scripts and any reference to python will call the binary directly. For interactive use it's probably best to call python2 directly if you rely on pre-3 syntax/functionality.

PS: AFAIK it's the same for zsh.