0

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
YorSubs
  • 3,194
  • 7
  • 37
  • 60
  • 2
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Sep 15 '21 at 05:54
  • @YorSubs : Wouldn't it be easier to create a function (say: `l`) instead, which accepts a drive letter and then performs this task? Wheter you type i.e. `lld` or `l d` does not make much difference. Both are 3 keystrokes. – user1934428 Sep 15 '21 at 06:41

1 Answers1

1

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.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70