Results 1 to 6 of 6
I want to make a command that will change the directory (using cd), then keep it that way when it returns to the command line. I am currently using bash ...
- 08-19-2008 #1Just Joined!
- Join Date
- Mar 2008
- Posts
- 34
making cd stay
I want to make a command that will change the directory (using cd), then keep it that way when it returns to the command line. I am currently using bash scripts /bin/bash.
- 08-19-2008 #2Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
Whether this is at all possible depends on what you call a "command". The working directory is bound to a process, and it is generally not possible to change the working directory of another process.
- 08-19-2008 #3
You can "make a command" by writing a script. This script can simply do the cd, and nothing else. If, say, the name of the file containing that script is fred, and that file is in your current directory, and your PATH variable includes "." (your current directory), then you can run that script by simply typing:
But as burschik points out, running a script like this will start a new process, change the current directory in that process, and finish the process. You'll be back at the bash prompt with nothing changed, because the process that's running the interactive bash didn't do the cd; the process running the shell script did, and that process is now gone.Code:fred
The solution is to type something else, not
Instead, you want to indicate to bash that you want the processing of the cd command to happen in the current process, instead of firing off a new process which would quickly disappear when the script was done. So instead you can say at the command prompt:Code:fred
or:Code:source fred
I typed that initial period in red so it would stand out. Make sure there's a space after it.Code:. fred
There's another way to do this in which you don't run a script every time you do the cd. It's to define an alias. You define that alias once each time you log in. You define it thus:
Then, any time you're still logged in, you can do that cd command just by typing this at the command line:Code:alias fred="cd whatever_directory_you_want"
No "source", no period.Code:fred
If you don't want to enter the alias command every time you log in, you can place that command in the file called .profile in your home directory.
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.
- 08-20-2008 #4Just Joined!
- Join Date
- Mar 2008
- Posts
- 34
That source or '.' thing was exactly what I needed thanks. It weird that it starts a new process when any command is run because you can't access the old process until the new one is done. Doesn't make sense.
- 08-21-2008 #5
You can run a process in the background, if you want:
This will execute the command and immediately return control to the shell, without waiting for it to finish. Unfortunately, you are in most cases unable to communicate with a backgrounded process by using the terminal.Code:command &
I'm curious what you mean by "it doesn't make sense". What is your particular use case that this is weird for?DISTRO=Arch
Registered Linux User #388732
- 08-22-2008 #6Just Joined!
- Join Date
- Mar 2008
- Posts
- 34
Never mind. Its fine. I understand why it has to follow the process.


Reply With Quote