Thread: How to get environment variable

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    How to get environment variable

    Hi,
    I want to get value stored in environment variable $FILEPATH,which is set as:
    $FILEPATH=/home/File
    I have used following code but it returning NULL......
    Code:
    char *var = getenv("$FILEPATH");
    
      printf ("The environment variable is: %s", var);
    When I echo it on command prompt ,then it gives me correct value but not in program.....

    Can anybody help in resolving this problem.......

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    remove the $ from your string when calling getenv.

    The $ is part of the shell's syntax to say "this is an environment variable", rather than part of the environment variables actual name.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    Thanks

    Thanks for help......it solved the problem......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. which variable can store words?
    By Hunterofman in forum C++ Programming
    Replies: 8
    Last Post: 04-28-2008, 05:59 PM
  3. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  4. Retrieving PATH Environment Variable
    By mercury529 in forum Windows Programming
    Replies: 9
    Last Post: 01-09-2007, 08:02 PM
  5. Win Console program environment variable
    By C3Pnuts in forum C Programming
    Replies: 2
    Last Post: 05-23-2005, 02:08 PM