Skip to main content

Julia Evans

« back to all TILs

Environment variables with no equals sign

I learned a long time ago that environment variables are literally represented as the string NAME=value (you can see this by running cat /proc/self/environ on Linux).

But what I never thought about until Kamal mentioned it to me yesterday was that you can technically put any string in your environment, it doesn’t have to have an equals sign.

Here’s a C program that does that and runs env:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main() {
    char *weird_env[] = {
        "NAME=value",
        "banana",
        NULL
    };
   char *argv[] = {"/usr/bin/env", NULL};
   execve("/usr/bin/env", argv, weird_env);
}

It prints out

NAME=value
banana

I don’t think this has any real practical implications. If you run a shell like bash with this “banana” variable it’ll just ignore it.