Pranjal Doshi
Pranjal Doshi

Reputation: 1262

change alias with directory bash

Is it possible to modify ~/.bashrc such that when I change directory alias also changes


e.g. When I am in directory /home/user/Desktop python3 alias is alias python3=/usr/bin/python3 and when I am in directory /home/user/Downloads python3 alias ispython3=/opt/conda/bin/python3

Upvotes: 0

Views: 83

Answers (1)

Barmar
Barmar

Reputation: 780919

Make it a shell function that checks the current directory and executes the appropriate version.

python3() {
    case "$PWD" in
    /home/user/Desktop) /usr/bin/python3 "$@" ;;
    /home/user/Downloads) /opt/conda/bin/python3 "$@" ;;
    *) /some/other/python3 "$@" ;;
    esac
}

Upvotes: 2

Related Questions