Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

#include <unistd.h>
int main()
    return 0;

I compiled it with following command (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
You could use an alias instead - alias proj='cd ~/enedil/projects/algo' – Michael Durrant Jul 8 '14 at 11:46
@MichaelDurrant oh, you meant cd ~/projects/algo? It is an idea! – enedil Jul 8 '14 at 11:48
This is by design! – Thorbjørn Ravn Andersen Jul 8 '14 at 16:01
up vote 14 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
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 '14 at 12:09
@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 '14 at 15:29

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

share|improve this answer

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

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


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

share|improve this answer

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
You can't run a C program this way, only a shell script. – John Kugelman Jul 9 '14 at 3:13

Your Answer


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.