LoginSignup
9
9

More than 5 years have passed since last update.

[C言語][Linux] 環境変数の値を取得する

Posted at

特定の値を取得するには、以下のように getenv() 関数を使えば良い

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

定義されている値を取得したい時は、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;
}

valは "TERM=xterm" の形で名前と値が"="で連結されているので、使う時にカットする必要がある

9
9
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9