Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (9.5k points)

I want to keep my script of  .bashrc and .bash_login files in version control to run in different OS. Can anyone tell me how to detect the OS in Bash script? 

1 Answer

0 votes
by (19.7k points)

Try the below code:

if [[ "$OSTYPE" == "linux-gnu"* ]]; then

        # ...

elif [[ "$OSTYPE" == "darwin"* ]]; then

        # Mac OSX

elif [[ "$OSTYPE" == "cygwin" ]]; then

        # POSIX compatibility layer and Linux environment emulation for Windows

elif [[ "$OSTYPE" == "msys" ]]; then

        # Lightweight shell and GNU utilities compiled for Windows (part of MinGW)

elif [[ "$OSTYPE" == "win32" ]]; then

        # I'm not sure this can happen.

elif [[ "$OSTYPE" == "freebsd"* ]]; then

        # ...

else

        # Unknown.

fi

If you want to learn more about Linuxthen go through this Linux tutorial by Intellipaat for more insights.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 4, 2021 in Linux by blackindya (18.4k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...