Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I wanted to make a simple C program to change to my project directory. Its content is following:

#include <unistd.h>
int main(){
    chdir("/home/enedil/projects/algo");
    return 0;
}

I compiled it with following commnd (pro.c is its file name):

gcc pro.c -o /usr/local/bin/pro

There were no errors nor warnings. But when I run it, it does not change directory. Why?

share|improve this question
1  
You could use an alias instead - alias proj='cd ~/enedil/projects/algo' –  Michael Durrant Jul 8 at 11:46
    
@MichaelDurrant oh, you meant cd ~/projects/algo? It is an idea! –  enedil Jul 8 at 11:48
1  
This is by design! –  Thorbjørn Ravn Andersen Jul 8 at 16:01
add comment

4 Answers 4

up vote 12 down vote accepted

Your program does exactly what you tell it to do: it changes the working directory for itself to /home/enedil/projects/algo. But once it exits, the shell's working directory is restored.

I guess what you want to achieve is to change the working directory of the parent process, i.e. the shell, without resorting to a simple cd. There is a method, but as its author says: don't do it.

share|improve this answer
7  
Nitpick, but the shell's working directory isn't restored, because it never changed. The shell is simply a process, just like the program is. Each program has it's own working directory. –  Patrick Jul 8 at 12:09
1  
@Patrick, true. Conceptually more accurate would be your working directory is restored, in that your process is "restored" (is active in the foreground). –  Paul Draper Jul 8 at 15:29
add comment

The current working directory is local to the process. So, what you want is not possible.

share|improve this answer
add comment

You can also use the CDPATH environmental variable. As explained in man bash:

   CDPATH The search path for the cd command.  This is  a  colon-separated
          list  of  directories  in  which the shell looks for destination
          directories specified by the cd  command.   A  sample  value  is
          ".:~:/usr".

So, for example, you could add this line to your ~/.profile:

CDPATH=~/projects

You can now run cd projects from any directory and move to ~/projects.

share|improve this answer
add comment

To do this for the shell, use the source command (aliased to .). It will run the application in the context of the current shell rather than creating a new shell context. This is how you can set environment variables as well. If you notice all of the startup scripts in /etc/rc.d or /etc/init.d, you will see lots of scripts that are included by other scripts with the command

. /etc/init.d/rcvars

The . causes the script /etc/init.d/rcvars so that anything it changes persists in the current environment.

share|improve this answer
1  
You can't run a C program this way, only a shell script. –  John Kugelman Jul 9 at 3:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.