3

I want to go to the HOME directory using C code. How do I access $HOME from the code?

4 Answers 4

5
#include <stdlib.h>
...
getenv("HOME")
Sign up to request clarification or add additional context in comments.

Comments

4

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

3

try getenv("HOME");

2 Comments

You definitely want double quotes.
Thanks. fixed. Been a long while since I did any C coding. Been stuck in PHP purgatory for far too long.
2

You should have double quotes. getenv("HOME");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.