スポンサーリンク

getenv()で環境変数の値を取得

getenv()で環境変数の値を取得することができます。

例えば、.bash_profileに、

$ vi ~/.bash_profile

export TMP='tmp'と設定したとします。

export TMP='tmp'

忘れずにsourceコマンドで更新をかけます。

$ source ~/.bash_profile

下記のように、getenvの引数に環境変数名を指定して、値を取得できます。

#include <stdlib.h>

        const char* envVal;
        envVal = getenv("TMP");

スポンサーリンク

サンプルコード

下記がサンプルコードになります。

$ cat sample.c 
#include <stdio.h>
#include <stdlib.h>

int main(){
	
        const char* envVal;
        envVal = getenv("TMP");
        
        printf("%s\n", envVal);
        	
	return 0;
}

下記が実行結果になります。
$ gcc -o sample sample.c
$ ./sample 
tmp

スポンサーリンク