How to check if a program is run in Bash on Ubuntu on Windows and not just plain Ubuntu?

后端 未结 10 627
时光说笑
时光说笑 2020-12-05 02:07

Pretty straightforward, the usual places to figure out the OS you\'re on seem to be identical to plain Ubuntu on Ubuntu for Windows. For example uname -a is ide

10条回答
  •  情歌与酒
    2020-12-05 02:41

    Here's what I put in my .bashrc

    if [[ $(uname -v | sed -rE 's/^#[0-9]{3,}-(\S+).+/\1/') == "Microsoft" ]]; then
      # WSL-specific code
    fi
    
    • uname -v gets the kernel version in the format of #379-Microsoft Wed Mar 06 19:16:00 PST 2019 and the sed expression pulls out the Microsoft string.

提交回复
热议问题