Find the answer to your Linux question:
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 ...
  1. #1
    Just 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.

  2. #2
    Linux 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.

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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:
    Code:
    fred
    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.

    The solution is to type something else, not
    Code:
    fred
    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:
    source fred
    or:
    Code:
    . fred
    I typed that initial period in red so it would stand out. Make sure there's a space after it.

    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:
    Code:
    alias fred="cd whatever_directory_you_want"
    Then, any time you're still logged in, you can do that cd command just by typing this at the command line:
    Code:
    fred
    No "source", no period.

    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.

  4. #4
    Just 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.

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    You can run a process in the background, if you want:
    Code:
    command &
    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.

    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

  6. #6
    Just Joined!
    Join Date
    Mar 2008
    Posts
    34
    Never mind. Its fine. I understand why it has to follow the process.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...