[C language] [Linux] Get the value of environment variable

To get a specific value, use the getenv () function as follows:

        const char *val;
        val = getenv("PATH");

When you want to get the defined value, retrieve the value of environ

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* environ */

int main(int argc, char **argv)
{
    extern char **environ;
    char **env = environ;

    while(*env) {
        const char *val;

        val = *env;
        if (val) {
            printf("%s\n", val);
        }
        env++;
    }

    return 0;
}

Since val has a name and value concatenated with "=" in the form of "TERM = xterm", it needs to be cut when using it.

Recommended Posts

[C language] [Linux] Get the value of environment variable
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller
Get the value of the middle layer of NN
Guidelines for reincarnating in the world of linux programming development (C / C ++ language)
Linux PATH environment variable
Get the variable name of the variable as a character string.
The story of building the fastest Linux environment in the world
[Django 2.2] Sort and get the value of the relation destination
How to limit the API to be published in the C language shared library of Linux
Get the number of digits
Get the output value of the command (as received by xargs)
Switch the setting value of setting.py according to the development environment
[Linux] [C / C ++] Summary of how to get pid, ppid, tid
100 language processing knock-29: Get the URL of the national flag image
Get the host name of the host PC with Docker on Linux
Find the definition of the value of errno
Get the number of views of Qiita
About the return value of pthread_mutex_init ()
Arrange the dirty environment variable PATH
Get the attributes of an object
About the return value of the histogram.
Get the first element of queryset
[Linux] [C / C ++] Get tid (thread id) / Wrap pthread_create to get tid of child thread
Get and set the value of the dropdown menu using Python and Selenium
Get the value from the [Django] Form
Get the number of Youtube subscribers
Periodically log the value of Omron environment sensor with Raspberry Pi
[Linux] Command to get a list of commands executed in the past
I measured 6 methods to get the index of the maximum value (minimum value) of the list
[C language] How to use the crypt function on Linux [Password hashing]
Logging the value of Omron environment sensor with Raspberry Pi (USB type)
Get the return value of an external shell script (ls) with python3
[Understanding in 3 minutes] The beginning of Linux
Implement part of the process in C ++
Get the column list & data list of CASTable
About the virtual environment of python version 3.7
[Python] Get the variable name with str
A quick overview of the Linux kernel
Build the execution environment of Jupyter Lab
Understand the "temporary" part of UNIX / Linux
[GCC] [C / C ++] [Linux] Dump of GCC predefined macros
[Linux] Learn the basics of shell commands
Get the last day of the specified month
[Python] Get the character code of the file
Get the filename of a directory (glob)
[PowerShell] Get the reading of the character string
Make the default value of the argument immutable
[NNabla] How to get the output (variable) of the middle layer of a pre-built network
[C language] Be careful of the combination of buffering API and non-buffering system call
linux / c> link> Get the execution result of the shell command in the C program> I was taught how to use popen ()
Get own process name at runtime in C / C ++ (for Linux)
[OS / Linux] Process, thread, memory management
[Linux] [C / C ++] Get tid (thread id) / Wrap pthread_create to get tid of child thread
Linux process
[C language] [Linux] Get the value of environment variable
Library for measuring execution time in Linux C applications
Get the contents of git diff from python
Note: Prepare the environment of CmdStanPy with docker
Prepare the execution environment of Python3 with Docker
Linux C / C ++ Build your own library creation environment
Check the type of the variable you are using
[Python] Get / edit the scale label of the figure
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
The origin of Manjaro Linux is "Mount Kilimanjaro"
Calculate the memory sharing rate of Linux processes