I want to go to the HOME directory using C code. How do I access $HOME from the code?
4 Answers
It's not just a bash variable, it's an environment variable. Have a look at getenv:
#include <stdlib.h>
...
// The value will be NULL if the variable doesn't exist
char* home = getenv("HOME");
Comments
try getenv("HOME");
2 Comments
Keith Thompson
You definitely want double quotes.
Marc B
Thanks. fixed. Been a long while since I did any C coding. Been stuck in PHP purgatory for far too long.