Wednesday, October 27, 2021

[SOLVED] change alias with directory bash

Issue

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


Solution

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
}


Answered By - Barmar