Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why chdir("~/dev/") returns -1?

Tags:

c++

c

unix

In my c++ application I want to change working directory to specified one. But when I specifying "~/dev/" as working directory chdir() fails and returns -1. Instead it works when I'm specifying "/home/myusername/dev". Why this happens? How can I solve this problem (except of checking if string first charachter is ~ and replacing it with /home/myusername)?

like image 619
Mihran Hovsepyan Avatar asked Aug 13 '26 09:08

Mihran Hovsepyan


1 Answers

The expansion of tilde (~) to HOME environment variable is done by bash. That expansion is done before that is passed to chdir command. The bottom line is ~ is handled by bash, not by chdir command. So you can not use it in chdir() function call.

In order to find other kind of expansions (and the details of ~ expansion) done by bash, check this link.

like image 143
taskinoor Avatar answered Aug 14 '26 22:08

taskinoor