2.12. Environment Variables#

Tools run in a restricted environment and do not inherit most environment variables from the parent process. You can set environment variables for the tool using EnvVarRequirement.

env.cwl#
#!/usr/bin/env cwl-runner
cwlVersion: v1.2
class: CommandLineTool
baseCommand: env
requirements:
  EnvVarRequirement:
    envDef:
      HELLO: $(inputs.message)
inputs:
  message: string
outputs:
  example_out:
    type: stdout
stdout: output.txt
echo-job.yml#
message: Hello world!

Now invoke cwltool with the tool description and the input object on the command line:

$ cwltool env.cwl echo-job.yml
INFO /home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide/envs/latest/bin/cwltool 3.1.20240404144621
INFO Resolved 'env.cwl' to 'file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide/checkouts/latest/src/_includes/cwl/environment-variables/env.cwl'
INFO [job env.cwl] /tmp/2tl503h2$ env > /tmp/2tl503h2/output.txt
INFO [job env.cwl] completed success
{
    "example_out": {
        "location": "file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide/checkouts/latest/src/_includes/cwl/environment-variables/output.txt",
        "basename": "output.txt",
        "class": "File",
        "checksum": "sha1$ead53d86effb2335a1d47e0f94ecb2b78a05f2c7",
        "size": 269,
        "path": "/home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide/checkouts/latest/src/_includes/cwl/environment-variables/output.txt"
    }
}INFO Final process status is success
$ cat output.txt
HOME=/tmp/2tl503h2
TMPDIR=/tmp/8y4es5cj
PATH=/home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide/envs/latest/bin:/home/docs/.asdf/shims:/home/docs/.asdf/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HELLO=Hello world!