Most efficient if statement in .zshrc to check whether Linux OS is running on WSL?












0















In my .zshrc file I conditionally set my PATH variable depending on whether I'm running on Linux or macOS - I'm now trying to figure out if there's a way I can efficiently detect from my .zshrc if I'm working on Linux running on WSL.



I'm wondering if I can somehow check for the existence of /mnt/c/Program Files or similar - but figure there must be a better way?



Example of my current .zshrc:



PATH="/usr/local/sbin:$PATH" 
if ! [[ "$OSTYPE" == "darwin"* ]]; then
export PATH="$HOME/.nodenv/bin:$HOME/.rbenv/bin:$PATH"
fi
eval "$(rbenv init -)"
eval "$(nodenv init -)"
PATH="$HOME/.bin:$PATH"
if [[ "$OSTYPE" == "darwin"* ]]; then
export ANDROID_SDK_ROOT="$HOME/Library/Android/sdk"
export PATH="$PATH:$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/tools/bin:$ANDROID_SDK_ROOT/platform-tools:$ANDROID_SDK_ROOT/build-tools:$ANDROID_SDK_ROOT/tools/lib/x86_64"
export PATH="$PATH:/usr/local/share/dotnet"
fi


If anyone has any better ideas than somehow checking for the existence of /mnt/c/Program Files I'd very much appreciate it!










share|improve this question























  • uname -r contains Microsoft. github.com/KittyKatt/screenFetch/blob/master/screenfetch-dev

    – Biswapriyo
    Jan 1 at 16:28











  • Thanks - that was exactly what I needed - popped an answer to my own question in below.

    – Michael Smith
    Jan 1 at 18:03
















0















In my .zshrc file I conditionally set my PATH variable depending on whether I'm running on Linux or macOS - I'm now trying to figure out if there's a way I can efficiently detect from my .zshrc if I'm working on Linux running on WSL.



I'm wondering if I can somehow check for the existence of /mnt/c/Program Files or similar - but figure there must be a better way?



Example of my current .zshrc:



PATH="/usr/local/sbin:$PATH" 
if ! [[ "$OSTYPE" == "darwin"* ]]; then
export PATH="$HOME/.nodenv/bin:$HOME/.rbenv/bin:$PATH"
fi
eval "$(rbenv init -)"
eval "$(nodenv init -)"
PATH="$HOME/.bin:$PATH"
if [[ "$OSTYPE" == "darwin"* ]]; then
export ANDROID_SDK_ROOT="$HOME/Library/Android/sdk"
export PATH="$PATH:$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/tools/bin:$ANDROID_SDK_ROOT/platform-tools:$ANDROID_SDK_ROOT/build-tools:$ANDROID_SDK_ROOT/tools/lib/x86_64"
export PATH="$PATH:/usr/local/share/dotnet"
fi


If anyone has any better ideas than somehow checking for the existence of /mnt/c/Program Files I'd very much appreciate it!










share|improve this question























  • uname -r contains Microsoft. github.com/KittyKatt/screenFetch/blob/master/screenfetch-dev

    – Biswapriyo
    Jan 1 at 16:28











  • Thanks - that was exactly what I needed - popped an answer to my own question in below.

    – Michael Smith
    Jan 1 at 18:03














0












0








0








In my .zshrc file I conditionally set my PATH variable depending on whether I'm running on Linux or macOS - I'm now trying to figure out if there's a way I can efficiently detect from my .zshrc if I'm working on Linux running on WSL.



I'm wondering if I can somehow check for the existence of /mnt/c/Program Files or similar - but figure there must be a better way?



Example of my current .zshrc:



PATH="/usr/local/sbin:$PATH" 
if ! [[ "$OSTYPE" == "darwin"* ]]; then
export PATH="$HOME/.nodenv/bin:$HOME/.rbenv/bin:$PATH"
fi
eval "$(rbenv init -)"
eval "$(nodenv init -)"
PATH="$HOME/.bin:$PATH"
if [[ "$OSTYPE" == "darwin"* ]]; then
export ANDROID_SDK_ROOT="$HOME/Library/Android/sdk"
export PATH="$PATH:$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/tools/bin:$ANDROID_SDK_ROOT/platform-tools:$ANDROID_SDK_ROOT/build-tools:$ANDROID_SDK_ROOT/tools/lib/x86_64"
export PATH="$PATH:/usr/local/share/dotnet"
fi


If anyone has any better ideas than somehow checking for the existence of /mnt/c/Program Files I'd very much appreciate it!










share|improve this question














In my .zshrc file I conditionally set my PATH variable depending on whether I'm running on Linux or macOS - I'm now trying to figure out if there's a way I can efficiently detect from my .zshrc if I'm working on Linux running on WSL.



I'm wondering if I can somehow check for the existence of /mnt/c/Program Files or similar - but figure there must be a better way?



Example of my current .zshrc:



PATH="/usr/local/sbin:$PATH" 
if ! [[ "$OSTYPE" == "darwin"* ]]; then
export PATH="$HOME/.nodenv/bin:$HOME/.rbenv/bin:$PATH"
fi
eval "$(rbenv init -)"
eval "$(nodenv init -)"
PATH="$HOME/.bin:$PATH"
if [[ "$OSTYPE" == "darwin"* ]]; then
export ANDROID_SDK_ROOT="$HOME/Library/Android/sdk"
export PATH="$PATH:$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/tools/bin:$ANDROID_SDK_ROOT/platform-tools:$ANDROID_SDK_ROOT/build-tools:$ANDROID_SDK_ROOT/tools/lib/x86_64"
export PATH="$PATH:/usr/local/share/dotnet"
fi


If anyone has any better ideas than somehow checking for the existence of /mnt/c/Program Files I'd very much appreciate it!







linux zsh windows-subsystem-for-linux zshrc






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 15:21









Michael SmithMichael Smith

66211




66211













  • uname -r contains Microsoft. github.com/KittyKatt/screenFetch/blob/master/screenfetch-dev

    – Biswapriyo
    Jan 1 at 16:28











  • Thanks - that was exactly what I needed - popped an answer to my own question in below.

    – Michael Smith
    Jan 1 at 18:03



















  • uname -r contains Microsoft. github.com/KittyKatt/screenFetch/blob/master/screenfetch-dev

    – Biswapriyo
    Jan 1 at 16:28











  • Thanks - that was exactly what I needed - popped an answer to my own question in below.

    – Michael Smith
    Jan 1 at 18:03

















uname -r contains Microsoft. github.com/KittyKatt/screenFetch/blob/master/screenfetch-dev

– Biswapriyo
Jan 1 at 16:28





uname -r contains Microsoft. github.com/KittyKatt/screenFetch/blob/master/screenfetch-dev

– Biswapriyo
Jan 1 at 16:28













Thanks - that was exactly what I needed - popped an answer to my own question in below.

– Michael Smith
Jan 1 at 18:03





Thanks - that was exactly what I needed - popped an answer to my own question in below.

– Michael Smith
Jan 1 at 18:03












3 Answers
3






active

oldest

votes


















1














There are many possible way to check WSL in any shell. Most reliable ways are:




  1. From uname -r command output.

  2. From /proc/version file.

  3. From /proc/sys/kernel/osrelease file.


#!/bin/bash

if uname -r |grep -q 'Microsoft' ; then
echo True
fi

if grep -q -i 'Microsoft' /proc/version ; then
echo True
fi

if grep -q -i 'Microsoft' /proc/sys/kernel/osrelease ; then
echo True
fi




Also there are many file existence can be checked with shell script. For example, only WSL has 1. /dev/lxss 2. /bin/wslpath 3. /sbin/mount.drvfs 4. /proc/sys/fs/binfmt_misc/WSLInterop 5. /etc/wsl.conf files but GNU/Linux distributions has not.



See more:




  • screenFetch

  • netfetch






share|improve this answer































    1














    In WSL, there is a special file for checking interoperability called /proc/sys/fs/binfmt_misc/WSLInterop which is WSL specific file. You can check using the following command:



    #!/bin/bash
    if [ -f /proc/sys/fs/binfmt_misc/WSLInterop ]; then
    echo True
    fi


    or more simple one-line code(in bash):



    [ -f /proc/sys/fs/binfmt_misc/WSLInterop ]


    This will return exit code 0 if true, exit code 1 if false.






    share|improve this answer































      0














      Thanks to Biswapiryo's comment - I came up with this solution to detect WSL:



      if [[ $(uname -r)] == ^*Microsoft$ ]]; then
      # Code goes here
      fi





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996607%2fmost-efficient-if-statement-in-zshrc-to-check-whether-linux-os-is-running-on-ws%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        There are many possible way to check WSL in any shell. Most reliable ways are:




        1. From uname -r command output.

        2. From /proc/version file.

        3. From /proc/sys/kernel/osrelease file.


        #!/bin/bash

        if uname -r |grep -q 'Microsoft' ; then
        echo True
        fi

        if grep -q -i 'Microsoft' /proc/version ; then
        echo True
        fi

        if grep -q -i 'Microsoft' /proc/sys/kernel/osrelease ; then
        echo True
        fi




        Also there are many file existence can be checked with shell script. For example, only WSL has 1. /dev/lxss 2. /bin/wslpath 3. /sbin/mount.drvfs 4. /proc/sys/fs/binfmt_misc/WSLInterop 5. /etc/wsl.conf files but GNU/Linux distributions has not.



        See more:




        • screenFetch

        • netfetch






        share|improve this answer




























          1














          There are many possible way to check WSL in any shell. Most reliable ways are:




          1. From uname -r command output.

          2. From /proc/version file.

          3. From /proc/sys/kernel/osrelease file.


          #!/bin/bash

          if uname -r |grep -q 'Microsoft' ; then
          echo True
          fi

          if grep -q -i 'Microsoft' /proc/version ; then
          echo True
          fi

          if grep -q -i 'Microsoft' /proc/sys/kernel/osrelease ; then
          echo True
          fi




          Also there are many file existence can be checked with shell script. For example, only WSL has 1. /dev/lxss 2. /bin/wslpath 3. /sbin/mount.drvfs 4. /proc/sys/fs/binfmt_misc/WSLInterop 5. /etc/wsl.conf files but GNU/Linux distributions has not.



          See more:




          • screenFetch

          • netfetch






          share|improve this answer


























            1












            1








            1







            There are many possible way to check WSL in any shell. Most reliable ways are:




            1. From uname -r command output.

            2. From /proc/version file.

            3. From /proc/sys/kernel/osrelease file.


            #!/bin/bash

            if uname -r |grep -q 'Microsoft' ; then
            echo True
            fi

            if grep -q -i 'Microsoft' /proc/version ; then
            echo True
            fi

            if grep -q -i 'Microsoft' /proc/sys/kernel/osrelease ; then
            echo True
            fi




            Also there are many file existence can be checked with shell script. For example, only WSL has 1. /dev/lxss 2. /bin/wslpath 3. /sbin/mount.drvfs 4. /proc/sys/fs/binfmt_misc/WSLInterop 5. /etc/wsl.conf files but GNU/Linux distributions has not.



            See more:




            • screenFetch

            • netfetch






            share|improve this answer













            There are many possible way to check WSL in any shell. Most reliable ways are:




            1. From uname -r command output.

            2. From /proc/version file.

            3. From /proc/sys/kernel/osrelease file.


            #!/bin/bash

            if uname -r |grep -q 'Microsoft' ; then
            echo True
            fi

            if grep -q -i 'Microsoft' /proc/version ; then
            echo True
            fi

            if grep -q -i 'Microsoft' /proc/sys/kernel/osrelease ; then
            echo True
            fi




            Also there are many file existence can be checked with shell script. For example, only WSL has 1. /dev/lxss 2. /bin/wslpath 3. /sbin/mount.drvfs 4. /proc/sys/fs/binfmt_misc/WSLInterop 5. /etc/wsl.conf files but GNU/Linux distributions has not.



            See more:




            • screenFetch

            • netfetch







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 1 at 19:41









            BiswapriyoBiswapriyo

            5041418




            5041418

























                1














                In WSL, there is a special file for checking interoperability called /proc/sys/fs/binfmt_misc/WSLInterop which is WSL specific file. You can check using the following command:



                #!/bin/bash
                if [ -f /proc/sys/fs/binfmt_misc/WSLInterop ]; then
                echo True
                fi


                or more simple one-line code(in bash):



                [ -f /proc/sys/fs/binfmt_misc/WSLInterop ]


                This will return exit code 0 if true, exit code 1 if false.






                share|improve this answer




























                  1














                  In WSL, there is a special file for checking interoperability called /proc/sys/fs/binfmt_misc/WSLInterop which is WSL specific file. You can check using the following command:



                  #!/bin/bash
                  if [ -f /proc/sys/fs/binfmt_misc/WSLInterop ]; then
                  echo True
                  fi


                  or more simple one-line code(in bash):



                  [ -f /proc/sys/fs/binfmt_misc/WSLInterop ]


                  This will return exit code 0 if true, exit code 1 if false.






                  share|improve this answer


























                    1












                    1








                    1







                    In WSL, there is a special file for checking interoperability called /proc/sys/fs/binfmt_misc/WSLInterop which is WSL specific file. You can check using the following command:



                    #!/bin/bash
                    if [ -f /proc/sys/fs/binfmt_misc/WSLInterop ]; then
                    echo True
                    fi


                    or more simple one-line code(in bash):



                    [ -f /proc/sys/fs/binfmt_misc/WSLInterop ]


                    This will return exit code 0 if true, exit code 1 if false.






                    share|improve this answer













                    In WSL, there is a special file for checking interoperability called /proc/sys/fs/binfmt_misc/WSLInterop which is WSL specific file. You can check using the following command:



                    #!/bin/bash
                    if [ -f /proc/sys/fs/binfmt_misc/WSLInterop ]; then
                    echo True
                    fi


                    or more simple one-line code(in bash):



                    [ -f /proc/sys/fs/binfmt_misc/WSLInterop ]


                    This will return exit code 0 if true, exit code 1 if false.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 2 at 12:21









                    Patrick Wu J.M.Patrick Wu J.M.

                    214




                    214























                        0














                        Thanks to Biswapiryo's comment - I came up with this solution to detect WSL:



                        if [[ $(uname -r)] == ^*Microsoft$ ]]; then
                        # Code goes here
                        fi





                        share|improve this answer




























                          0














                          Thanks to Biswapiryo's comment - I came up with this solution to detect WSL:



                          if [[ $(uname -r)] == ^*Microsoft$ ]]; then
                          # Code goes here
                          fi





                          share|improve this answer


























                            0












                            0








                            0







                            Thanks to Biswapiryo's comment - I came up with this solution to detect WSL:



                            if [[ $(uname -r)] == ^*Microsoft$ ]]; then
                            # Code goes here
                            fi





                            share|improve this answer













                            Thanks to Biswapiryo's comment - I came up with this solution to detect WSL:



                            if [[ $(uname -r)] == ^*Microsoft$ ]]; then
                            # Code goes here
                            fi






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 1 at 18:03









                            Michael SmithMichael Smith

                            66211




                            66211






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996607%2fmost-efficient-if-statement-in-zshrc-to-check-whether-linux-os-is-running-on-ws%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                Npm cannot find a required file even through it is in the searched directory