Bash function to alias python command with ipython when no arguments are used

119 Views Asked by At

I would like to alias python with ipython only when no arguments are follows so that I can use ipython's shell and its autocompletion feature. For example:

#This should start the ipython shell
python 

#These should run python as usual
python -c "print(100)" #run print in python
python --version #run python --version 

I am trying with something like

python() {
if [ "$#" -eq 0 ]; 
then
    ipython
else
    eval $(printf "%q " "python $@")     #python --help: command not found
    #eval $(printf 'python %s\n' "$@")   #infinite loop calling python function
fi
}
1

There are 1 best solutions below

3
Paul Evans On BEST ANSWER

No need for the eval. This function uses env to run the "default" python command

function python() {
    if [ "$#" -eq 0 ]; 
    then
        ipython
    else
        env python "$@" # will run python wherever it's installed
    fi
}