How to check if environment variable is set from C program

pleaver

I'm currently working on a program, and I would like to print special output if an environment variable is set.

For example, suppose I want environment variable "DEBUG".

In my bash command prompt, I set DEBUG by typing the command: DEBUG=

Then in my C program, I can verify this environment variable is set by printing out all the content of char **environ. DEBUG does show up in this environment printout.

However, I don't know how to retrieve this environment variable for conditional checking. I've tried using the function getenv like so:

getenv("DEBUG")

If I were to try to print out this output like below I get a seg fault:

printf("get env: %s\n", getenv("DEBUG"));

I even tried this on a known environment variable like "HOME":

printf("get env: %s\n", getenv("HOME"));

which still produces a seg fault.

Does any one have any experience checking if an environment variable is set from a C program? I'm having issues even pulling a single environment variable which is preventing me from doing so.

Wintermute

getenv returns NULL when the environment variable for which it is asked is not set. Your check could thus simply be

if(getenv("DEBUG")) {
  // DEBUG is set
} else {
  // DEBUG is not set
}

Note that there is a difference between shell and environment variables; if you want a variable to show up in the environment of a shell's subprocess, you have to export it in the shell:

export DEBUG=some_value

or

DEBUG=some_value
export DEBUG

It is not enough to just say DEBUG=some_value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change environment variable in shell executing a C program from that C program?

From Dev

How to check if a environment variable is set with 'set -o nounset' ?

From Dev

How to set Jenkins environment variable from script

From Dev

How to set `screen` environment variable from bash?

From Dev

How can I consistently set an environment variable for a single program?

From Dev

How to set system environment variable in C#?

From Dev

How do I check if an environment variable is set in cmake

From Dev

How can I check if an environment variable is set in Node.js?

From Dev

How to check that environment variable is set but also really exists?

From Dev

How to set a variable with an environment variable read from a text file

From Dev

Windows Batch Command : How to dereference FOR loop variable to check if that variable is SET in Environment Variable

From Dev

Windows Batch Command : How to dereference FOR loop variable to check if that variable is SET in Environment Variable

From Dev

Environment variable not set, from Netbeans

From Dev

set environment variable from batch

From Dev

Environment variable not set, from Netbeans

From Dev

Set environment variable with zeroes in C

From Dev

How do I set a Machine environment variable from a Chocolatey package?

From Dev

How to set a global environment variable from Inno Setup installer?

From Dev

How to set Windows environment variable from Tcl script

From Dev

How do I set a environment variable from a powershell command?

From Dev

How to set shell environment variable from autotools .am file?

From Dev

How do I set environment variable by importing from a file?

From Dev

How to set environment variable Path using C#

From Dev

How to read Environment variable set by Travis CI in C#

From Java

How to set GOPRIVATE environment variable

From Dev

How to set an integer as a environment variable?

From Dev

How to set environment variable in java

From Dev

How to set the environment variable in tox?

From Dev

How to permanently set an environment variable

Related Related

  1. 1

    How to change environment variable in shell executing a C program from that C program?

  2. 2

    How to check if a environment variable is set with 'set -o nounset' ?

  3. 3

    How to set Jenkins environment variable from script

  4. 4

    How to set `screen` environment variable from bash?

  5. 5

    How can I consistently set an environment variable for a single program?

  6. 6

    How to set system environment variable in C#?

  7. 7

    How do I check if an environment variable is set in cmake

  8. 8

    How can I check if an environment variable is set in Node.js?

  9. 9

    How to check that environment variable is set but also really exists?

  10. 10

    How to set a variable with an environment variable read from a text file

  11. 11

    Windows Batch Command : How to dereference FOR loop variable to check if that variable is SET in Environment Variable

  12. 12

    Windows Batch Command : How to dereference FOR loop variable to check if that variable is SET in Environment Variable

  13. 13

    Environment variable not set, from Netbeans

  14. 14

    set environment variable from batch

  15. 15

    Environment variable not set, from Netbeans

  16. 16

    Set environment variable with zeroes in C

  17. 17

    How do I set a Machine environment variable from a Chocolatey package?

  18. 18

    How to set a global environment variable from Inno Setup installer?

  19. 19

    How to set Windows environment variable from Tcl script

  20. 20

    How do I set a environment variable from a powershell command?

  21. 21

    How to set shell environment variable from autotools .am file?

  22. 22

    How do I set environment variable by importing from a file?

  23. 23

    How to set environment variable Path using C#

  24. 24

    How to read Environment variable set by Travis CI in C#

  25. 25

    How to set GOPRIVATE environment variable

  26. 26

    How to set an integer as a environment variable?

  27. 27

    How to set environment variable in java

  28. 28

    How to set the environment variable in tox?

  29. 29

    How to permanently set an environment variable

HotTag

Archive