Calling $OSTYPE in a linux VM only returns "\n" and not the os type

In one of my unit test for my module I have the following python code:

subprocess.check_output("echo $OSTYPE", shell=True).decode("utf8")

This portion the code returns a “\n” character, but no the OSTYPE. Could you help me figure out how to run $OSTYPE within a python script in circleci?

Is $OSTYPE a standard Linux environment variable, or a CircleCI variable, or something you have set up?

It is a standard Linux environment variable

OK. Is it available in your build server? I take it you are using the Docker executor? What is your parent image?

One approach you can take here is to run a set command in a step on its own, this will dump all env vars. See if the one you want is in there first. Python definitely won’t be able to see it if it is not in there!

1 Like

TIL about $OSTYPE, can confirm this works locally on both my Mac and Debian boxes.

Can you share your .circleci/config.yml so we can get a better idea of the environment you are running this in?

Here is a portion of my config. (This is the part that is failing on the $OSTYPE call.) the checkout and setup steps work. The error occurs during the run step while running the unit tests.

version: 2

variables:
  setup_p2: &setup_p2
    run:
      shell: /bin/bash
      name: Setup
      command: bash .circleci/setup.sh 
  linux: &linux
    machine: true 

jobs:
  test-linux:
    <<: *linux
    steps:
      - checkout
      - *setup
      - run: 
          shell: /bin/bash
          name: ggd unit tests (Linux)
          command: py.test tests/ --duration=0 -v
          no_output_timeout: 9900

workflows:
  version: 2
  ggd-unit-tests:
    jobs:
      - test-linux

In the setup step the $OSTYPE environment variable is available. I use it to distinguish between macos and linux. Here is a snippet from my setup.sh script:

if [[ $OSTYPE == darwin* ]]; then
    tag="MacOSX"
    tag2="darwin"
elif [[ $OSTYPE == linux* ]]; then
    tag="Linux"
    tag2="linux"

fi 

However, it does not work during the run step.

I can confirm this env exists in the machine executor here https://circleci.com/gh/drazisil/circleci-testing/515

There must be something in python that is annoying it and causing it to be blank. I’m not well versed in python so would love other thoughts. Are you using the same version of python locally?