When writing a bash script, how to know whether you are on WSL or not? This affects some things, such as the location of your ~/Documents folder.

For example, suppose you are copying file1 to the ~/Documents folder, which is /mnt/c/Users/$USER/Documents under WSL. Here are two ways to do this.

The explicit way

docs=~/Documents
grep 'Microsoft' /proc/version > /dev/null 2>&1 && docs=/mnt/c/Users/$USER/Documents
cp file1 $docs/

The implicit way

sync="cp file1 ~/Documents/"
sync_wsl="cp file1 /mnt/c/Users/$USER/Documents/"
eval $sync || eval $sync_wsl

I like the implicit way. It doesn’t require the knowledge of the Linux version info.