How can you access an environment variable that has a space in its name in bash?

Running env returns “Clear Workspace=true”. How can I access it in bash? FYI, it’s coming from a Jenkins Parameterized Build parameter name. ${Clear Workspace} does not appear to work.

Also, how is Jenkins even able to create this environment variable? Running Clear Workspace=true in bash obviously doesn’t work as it tries to run the “Clear” command with an argument of “Workspace=true”.

  • How to disable Git Credential Manager for Windows?
  • How do I exclude files from git ls-files?
  • Function in bash to commit and push in one command
  • SSH - ksh: git: not found
  • How to use posh-git-bash in oh-my-zsh?
  • Why does my string not split nicely into an array?
  • I could of course make the job parameter name Clear_Workspace, but it’s presented in a form to the user, so I’d rather not. Also, the Maven Build Plugin for Jenkins has several parameter names with spaces in them, so it must be possible to access them somehow.

  • How to get the job name on a groovy dynamic parameter in Jenkins
  • How to send “back to normal” notifications in Jenkins Declarative Pipeline?
  • Puppet exec command runs in shell, but not via puppet
  • Maven release plugin - SNAPSHOT project needed
  • Rebuild Docker container on file changes
  • Jenkins Workflow Checkout Accessing BRANCH_NAME and GIT_COMMIT
  • 5 Solutions collect form web for “How can you access an environment variable that has a space in its name in bash?”

    You can simulate this bit of fun with the env command

    env Clear\ Workspace=true bash
    

    That will give you a shell with the environment variable set.

    A hacky way to get the environment variable value back out is:

    declare -p Clear\ Workspace | sed -e "s/^declare -x Clear Workspace=\"//;s/\"$//" 
    

    Other than that you’d need to use either a native code program or a scripting language to pull it out, e.g.

    ruby -e "puts ENV['Clear Workspace']"
    

    Which is much less hacky… also if you don’t have ruby

    perl -e 'print "$ENV{\"Clear Workspace\"}\n";'
    

    also

    python -c 'import os; print os.environ["Clear Workspace"]'
    

    And here is a native code version:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char **argv, char **envp) 
    {
      char **env;
      char *target;
      int len;
      if (argc != 2) 
      {
        printf("Syntax: %s name\n", argv[0]);
        return 2;
      }
      len = strlen(argv[1]);
      target = calloc(len+2,sizeof(char));
      strncpy(target,argv[1],len+2);
      target[len++] = '=';
      target[len] = '0';
      for (env = envp; *env != 0; env++) 
      {
        char *thisEnv = *env;
        if (strncmp(thisEnv,target,len)==0)
        {
          printf("%s\n",thisEnv+len);
          return 0;
        }
      }
      return 1;
    }
    

    Jenkins is probably creating something other than an environment variable.

    You cannot have spaces in environment variables. Quoting http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html:

    Environment variable names used by the utilities in the Shell and
    Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase
    letters, digits, and the ‘_’ (underscore) from the characters defined
    in Portable Character Set and do not begin with a digit.

    bash is not the only language that can manipulate the environment:

    $ perl -e '$ENV{"Clear Workspace"}="true"; system "env"' | grep Clear
    Clear Workspace=true
    

    If you’re in a shell, you can always parse the output of env (untested)

    value=$(env | while IFS="=" read -r var value; do
                      if [[ $var = "Clear Workspace" ]]; then
                          echo "$value"
                          break
                      fi
                  done )
    

    Java supports passing environment variables with spaces in names to processes. Here’s an example that proves it:

    Given /tmp/test

    #!/bin/sh
    env > /tmp/test.out
    

    and Test.java

    import java.util.*;
    import java.io.*;
    
    public class Test {
        public static void main(String[] args) throws Exception     {
            ProcessBuilder pb = new ProcessBuilder("/tmp/test");
            Map<String, String> env = pb.environment();
            env.put("a b", "true");
            pb.directory(new File("/tmp"));
            Process p = pb.start();
        }
    }
    

    run

    > javac Test.java
    > java Test
    > grep "a b" /tmp/test.out 
    a b=true
    

    Jenkins uses the same functionality to start processes.

    For you to access those from a pure shell, without using python, ruby, etc however, I am not sure. I haven’t been able to achieve it yet.

    You may wish to convert those to variable without spaces as those have better portability guarantee.

    The Following command will return the value of the environment variable ” Clear Workspace “:

    set | grep "Clear Workspace" | awk -F'=' '{print $NF}'
    

    You can assign to another variable without space and use for your purpose :

    export Clear_Workspace=\`set | grep "Clear Workspace" | awk -F'=' '{print $NF}'\`
    

    Give a try.

    Git Baby is a git and github fan, let's start git clone.