putenv
putenv(string $assignment): bool
Mutates this process's environment block with "NAME=value" syntax (add or overwrite); a bare "NAME" without "=" deletes. The change affects only this process and children spawned afterwards, and is restored at the end of the request (web context).
C C23
#include <stdio.h>
#include <stdlib.h>
int main(void) {
/* POSIX: setenv overwrites, unsetenv removes; children inherit the result */
setenv("CODEABC_DEMO_REGION", "eu", 1);
printf("%s\n", getenv("CODEABC_DEMO_REGION"));
setenv("CODEABC_DEMO_REGION", "us", 1);
printf("%s\n", getenv("CODEABC_DEMO_REGION"));
unsetenv("CODEABC_DEMO_REGION");
printf("%s\n", getenv("CODEABC_DEMO_REGION") == NULL ? "removed" : "still set");
return 0;
}
eu us removed
C++ C++26
#include <cstdlib>
#include <iostream>
int main() {
// POSIX setenv/unsetenv - the C++ standard itself has no environment API
setenv("CODEABC_DEMO_REGION", "eu", 1);
std::cout << std::getenv("CODEABC_DEMO_REGION") << "\n";
setenv("CODEABC_DEMO_REGION", "us", 1);
std::cout << std::getenv("CODEABC_DEMO_REGION") << "\n";
unsetenv("CODEABC_DEMO_REGION");
std::cout << (std::getenv("CODEABC_DEMO_REGION") == nullptr ? "removed" : "still set") << "\n";
return 0;
}
eu us removed
C# v11.0.100
// C#: SetEnvironmentVariable(process) mutates this process; a null value removes
Environment.SetEnvironmentVariable("CODEABC_DEMO_REGION", "eu");
Console.WriteLine(Environment.GetEnvironmentVariable("CODEABC_DEMO_REGION"));
Environment.SetEnvironmentVariable("CODEABC_DEMO_REGION", "us");
Console.WriteLine(Environment.GetEnvironmentVariable("CODEABC_DEMO_REGION"));
Environment.SetEnvironmentVariable("CODEABC_DEMO_REGION", null); // null removes
Console.WriteLine(Environment.GetEnvironmentVariable("CODEABC_DEMO_REGION") ?? "removed");
eu us removed
Go v1.27.4
package main
import (
"fmt"
"os"
)
func main() {
// Go: os.Setenv writes this process's environment; Unsetenv removes
os.Setenv("CODEABC_DEMO_REGION", "eu")
fmt.Println(os.Getenv("CODEABC_DEMO_REGION"))
os.Setenv("CODEABC_DEMO_REGION", "us")
fmt.Println(os.Getenv("CODEABC_DEMO_REGION"))
os.Unsetenv("CODEABC_DEMO_REGION")
_, ok := os.LookupEnv("CODEABC_DEMO_REGION")
fmt.Println("still visible:", ok)
}
eu us still visible: false
Java v27.0.1
public class Main {
public static void main(String[] args) {
// Java cannot modify its own environment - ProcessBuilder only shapes a child's
System.out.println(System.getenv("CODEABC_DEMO_REGION") == null ? "own env unchanged" : "changed");
ProcessBuilder pb = new ProcessBuilder("echo", "child"); // never started - only the env map
pb.environment().put("CODEABC_DEMO_REGION", "eu");
System.out.println("child would see: " + pb.environment().get("CODEABC_DEMO_REGION"));
}
}
own env unchanged child would see: eu
JavaScript v26.13.1
// Node: assigning to process.env mutates this process's environment
process.env.CODEABC_DEMO_REGION = "eu";
console.log(process.env.CODEABC_DEMO_REGION);
process.env.CODEABC_DEMO_REGION = "us";
console.log(process.env.CODEABC_DEMO_REGION);
delete process.env.CODEABC_DEMO_REGION; // children spawned from here on no longer see it
console.log(process.env.CODEABC_DEMO_REGION); // undefined
eu us undefined
PHP v8.6.0
// putenv mutates this process's environment block ("NAME=value" syntax)
var_dump(putenv("CODEABC_DEMO_REGION=eu"));
echo getenv("CODEABC_DEMO_REGION"), "\n";
putenv("CODEABC_DEMO_REGION=us"); // overwrite
echo getenv("CODEABC_DEMO_REGION"), "\n";
putenv("CODEABC_DEMO_REGION"); // no "=": remove
var_dump(getenv("CODEABC_DEMO_REGION"));
bool(true) eu us bool(false)
Python v3.15.2
# Python: os.environ writes go through putenv; del removes the name
import os
os.environ["CODEABC_DEMO_REGION"] = "eu"
print(os.environ["CODEABC_DEMO_REGION"])
os.environ["CODEABC_DEMO_REGION"] = "us"
print(os.environ["CODEABC_DEMO_REGION"])
del os.environ["CODEABC_DEMO_REGION"]
print(os.getenv("CODEABC_DEMO_REGION")) # None after removal
eu us None
Rust v1.99.1
fn main() {
// Rust: set_var/remove_var mutate this process's environment
std::env::set_var("CODEABC_DEMO_REGION", "eu");
println!("{}", std::env::var("CODEABC_DEMO_REGION").unwrap());
std::env::set_var("CODEABC_DEMO_REGION", "us");
println!("{}", std::env::var("CODEABC_DEMO_REGION").unwrap());
std::env::remove_var("CODEABC_DEMO_REGION");
println!("still visible: {}", std::env::var("CODEABC_DEMO_REGION").is_ok());
}
eu us still visible: false
TypeScript v26.13.1
// TS: same as JS - process.env mutations are process-local
process.env.CODEABC_DEMO_REGION = "eu";
console.log(process.env.CODEABC_DEMO_REGION);
process.env.CODEABC_DEMO_REGION = "us";
console.log(process.env.CODEABC_DEMO_REGION);
delete process.env.CODEABC_DEMO_REGION;
console.log(process.env.CODEABC_DEMO_REGION); // undefined
eu us undefined
Language differences
C: POSIX setenv (with an overwrite switch) / putenv (the string may become part of the environment) / unsetenv; children inherit via fork+exec.
C++: the standard library has no environment API - use the POSIX family.
C#: SetEnvironmentVariable(name, null) deletes; the target defaults to Process (this process only).
Go: os.Setenv/Unsetenv; children from syscall.Exec inherit the current environment.
Java: CANNOT modify its own environment - a hard JVM limit; only ProcessBuilder.environment() can pre-set a child's.
JavaScript: process.env assignment / delete; spawned children inherit by default (pass env to replace it wholesale).
PHP: putenv touches only the real environment block - the $_ENV superglobal is a separate copy and the two do not sync; putenv("NAME") deletes; the environment is restored at the end of a web request; CLI children (e.g. exec) inherit the changes - the classic way to pass config to a subprocess.
Python: os.environ["X"]="v" and os.putenv both write; del os.environ["X"] removes; os.environ also keeps a Python-side cache.
Rust: set_var/remove_var map directly to putenv/unsetenv; unsafe since edition 2024.
TypeScript: same as JavaScript.