bashaliaswindows-subsystem-for-linuxmount-point

WSL bash, create aliases for each available Windows drive


For my WSL setup, in my .bashrc, I have the following:

[ -d /mnt/c ] && alias llc='cd /mnt/c && ll'
[ -d /mnt/d ] && alias lld='cd /mnt/d && ll'
[ -d /mnt/e ] && alias lle='cd /mnt/e && ll'

This is fine to quickly jump to each Windows drive, but is it possible to make this generic such that it tries to do create aliases like these for any drive letter from c to z that it sees on startup? e.g. something like:

# this is not real code:
for i in [a-z]; do
    [ -d /mnt/$(i) ] && alias ll$(i)='cd /mnt/$(i) && ll'
done

Solution

  • Your pseudo-code is close. You just need to iterate over the globbed directories themselves and, as pointed out in the comments, use double-quotes to allow for variable interpolation:

    for d in /mnt/[a-z]
    do
      alias "ll$(basename ${d})"="cd $d && ll"
    done
    

    On the first pass, $d=/mnt/a and $(basename ${d}) is a, and so on.