It depends upon your shell. If you use bash
-see bash(1) for more- type
export EXAMPLEVAR="Hi"
in the shell (e.g. in the terminal before running your program), then use getenv("EXAMPLEVAR")
in your C program. See getenv(3)
If you don't export
a bash variable foo
, you still can use $foo
in your bash commands, but getenv("foo")
would fail and return NULL from inside compiled C programs.
Conventionally, environment variables have full capital names. See environ(7)
See also env(1) command.