Search code examples
linuxshelllscd

shell script to call ls command every time i change directories


I am wondering if there is any way to make a script, that would run in the background and which will call the "ls" command every time I change directories("cd") in Linux.

I know that in order to put a process in the background you add a "&" when you run it.

Thanks in advance!


Solution

  • You could replace cd with a shell function in your ~/.bashrc or similar startup script:

    function cd {
        builtin cd "$@"
        RET=$?
        ls
        return $RET
    }
    

    this would also return the exit code of cd, just in case...

    builtin is a shell builtin to execute the shell builtin cd instead of the cd funciton, to avoid running into a recursive loop - at least in bash - but should also work with other shells...