This is the documentation for the latest (main) development branch of Zephyr. If you are looking for the documentation of previous releases, use the drop-down menu on the left and select the desired version.

POSIX Environment Variables


In this sample application, the POSIX setenv(), function is used to populate several environment variables in C. Then, all environment variables are then printed.

If the user sets a new value for the ALERT environment variable, it is printed to standard output, and then cleared via unsetenv().

Building and Running

This project outputs to the console. It can be built and executed on QEMU as follows:

west build -b qemu_riscv32 samples/posix/env
west build -t run

Sample Output

The program below shows sample output for a specific Zephyr build.


Setting Environment Variables

The shell command below shows how to create a new environment variable or update the value associated with an existing environment variable.

The C code that is part of this sample application displays the value associated with the ALERT environment variable and then immediately unsets it.

uart:~$ posix env set ALERT="Happy Friday!"
uart:~$ ALERT="Happy Friday!"
uart:~$ posix env set HOME=""

Getting Environment Variables

The shell command below may be used to display the value associated with one environment variable.

uart:~$ posix env get BOARD

The shell command below may be used to display all environment variables and their associated values.

uart:~$ posix env get

Unsetting Environment Variables

The shell command below may be used to unset environment variables.

uart:~$ posix env unset BOARD