Search code examples
bashjenkinsos-detection

Where does the OS take the value for $OSTYPE


I have a legacy script that depends on the value of $OSTYPE variable. What this script expects is that on Solaris the value is "SunOS" while on AIX it is "AIX". For Linux it checks the variable against the regex where the uppercased value starts with "LINUX".

I've tried to run this script from Jenkins, and discovered that the values are different: Solaris returns "solaris2.11" while AIX returns "aix7.1.0.0". I ran the echo $OSTYPE command on exactly the same servers where Jenkins runs the script, and I get "SunOS"/"AIX" instead.

So the main question I have is: what is the source of this difference (how does the system form the value of this variable, and why does Jenkins have different values than just echo ran from bash). A side question is which is the reliable way to use $OSTYPE.


Solution

  • The variable OSTYPE stores the value of OS TYPE in the shell variables. It depends on shell what value it stores.

    For example, if you are using bash, you will get one value of OSTYPE variable, in zsh, some other value and in sh, you may not get any value itself.

    See the difference below in using bash and zsh.

    bash

    $ echo $OSTYPE
      darwin18
    

    zsh

    % echo $OSTYPE
      darwin18.0
    

    So, you may need to check what shell jenkins is using when you are getting different values of OSTYPE