Alias python3 to python in bash shell

When working with Python, it can be convenient to alias the command ‘python3’ to simply ‘python’ in the bash shell. This allows you to use the shorter command ‘python’ instead of ‘python3’ when running Python scripts. In this article, we will explore three different ways to achieve this aliasing in the bash shell.

Option 1: Using the alias command

The simplest way to alias ‘python3’ to ‘python’ is by using the alias command in the bash shell. Open your terminal and type the following command:

alias python=python3

This command creates an alias named ‘python’ that points to the ‘python3’ command. Now, whenever you type ‘python’ in the terminal, it will execute the ‘python3’ command instead.

Option 2: Modifying the .bashrc file

If you want the alias to persist across terminal sessions, you can add it to your .bashrc file. Open the .bashrc file in a text editor and add the following line:

alias python=python3

Save the file and exit the text editor. Now, whenever you open a new terminal session, the alias will be automatically set.

Option 3: Creating a bash function

Another way to achieve the aliasing is by creating a bash function. Open your .bashrc file and add the following lines:

python() {
    python3 "$@"
}

This bash function is named ‘python’ and it calls the ‘python3’ command with any arguments passed to it. Save the file and exit the text editor. Now, whenever you type ‘python’ in the terminal, it will execute the ‘python3’ command.

After exploring these three options, the best choice depends on your specific needs. If you only want a temporary alias, option 1 using the alias command is the simplest and most straightforward. If you want the alias to persist across terminal sessions, option 2 modifying the .bashrc file is the way to go. Lastly, if you prefer a more flexible solution that allows you to pass arguments to the aliased command, option 3 creating a bash function is the best choice.

Rate this post

10 Responses

    1. I couldnt disagree more. Option 2 is a recipe for disaster. Messing with .bashrc is like adding unnecessary complications to your shell. Stick to the basics and keep it simple. No need for unnecessary spice.

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents