Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

jenkins

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".

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.

like image 667
Spycho Avatar asked Aug 27 '13 14:08

Spycho


People also ask

Can environment variables have spaces?

Most programs separate their command line arguments with a space. But the PATH environment variable doesn't use spaces to separate directories. It uses semicolons.

How do I see environment variables in bash?

Under bash shell: To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.

How do I pass an environment variable in bash script?

Environment Variables Bash scripts can also be passed with the arguments in the form of environment variables. This can be done in either of the following ways: Specifying the variable value before the script execution command. Exporting the variable and then executing the script.

How do I see environment variables in terminal?

To display the values of environment variables, use the printenv command. If you specify the Name parameter, the system only prints the value associated with the variable you requested.


2 Answers

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, which should work up to bash 4.0, to get the environment variable value back out is:

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

Bash versions starting with 4.0 will instead return an error and are unable to extract such environment variables in that way.

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;
}
like image 166
Stephen Connolly Avatar answered Oct 18 '22 17:10

Stephen Connolly


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 )
like image 30
glenn jackman Avatar answered Oct 18 '22 19:10

glenn jackman