Write a c program to print all environment variables in Linux using c programming. Environmental variables are stored in system. It provide a way to influence the behavior of software program on the system.

c program to print all environment variables In Linux

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

extern char** environ;

int main() {
	char** env = environ;
	while (env) {
		printf("%s\n", *env);
		env++;
	}
	return 0;
}

output:

( here is some output )

GNOME_KEYRING_PID=1827
USER=bosch
LANGUAGE=en_IN:en
SESSION=ubuntu
SHLVL=0
HOME=/home/bosch
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin
DISPLAY=:0
LANG=en_IN
SHELL=/bin/bash
......

The environment variables are strings that represent key-value pairs. For example HOME is Linux environmental variable. The HOME variable represents linux user’s home directory.

HOME=/home/bosch

Look at the below PATH variable.

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin

From PATH and its value we can notice that a environmental variable can contain more than one value by using “:”

  • We can also check the value of Linux environmental variable from shell command echo.

print all environment variables in Linux using c programming



Related Contents to follow