Find the answer to your Linux question:
Results 1 to 9 of 9
Hi, I have a bash script that I want to change the directory in. Example: I am in the /root/ directory. I run cdb (my script) and when the script ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2005
    Posts
    46

    Change directory using script


    Hi,

    I have a bash script that I want to change the directory in.

    Example: I am in the /root/ directory. I run cdb (my script) and when the script finishes, I want to be in the /var/log/ directory.

    As of now, my script cd to /var/log but only within the context of the script.

    How do I make it change the directory of my TTY when the script finishes?

    Thanks.

  2. #2
    Linux Engineer
    Join Date
    Mar 2005
    Posts
    1,431
    Run the file like this:
    Code:
    . scriptname
    instead of like this:
    Code:
    ./scriptname
    I think it will work.

  3. #3
    Just Joined!
    Join Date
    Feb 2005
    Posts
    46
    Thanks jaboua!

    That works. However, my script is suppose to be a enhanced version of cd and I would like to do it in the script. I don't want to have to type: . cdb everytime.

  4. #4
    Linux Engineer
    Join Date
    Mar 2005
    Posts
    1,431
    Maybe you can try making an "alias" so you type "cd", but it executes ". /home/user/bin/cdb".

  5. #5
    Just Joined!
    Join Date
    Feb 2005
    Posts
    46
    Good idea. I didn't think about that.

    Thank you so much!

  6. #6
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    The reason your cdb script doesn't work the way you want is that it's executed in a subshell with its own enviroment, the starting shell's environment is preserved and restored when your script finishes. "dotting" the script run its content in the current shell environment. An alternative is to write a shell function, such as:
    Code:
    _cd()
    {
        'cd' $*
        PS1="$(/usr/bin/id -unr)@$HOST:${PWD}\$ "
    }
    
    alias cd=cd_

  7. #7
    Just Joined!
    Join Date
    Jan 2007
    Posts
    1

    Arrow the source code !!

    can u post the code for changing the cwd to some other
    in bash /csh environment !



    Quote Originally Posted by helpmhost
    Hi,

    I have a bash script that I want to change the directory in.

    Example: I am in the /root/ directory. I run cdb (my script) and when the script finishes, I want to be in the /var/log/ directory.

    As of now, my script cd to /var/log but only within the context of the script.

    How do I make it change the directory of my TTY when the script finishes?

    Thanks.

  8. #8
    Just Joined!
    Join Date
    Jun 2011
    Posts
    1
    Interesting thread - thanks for the info!

    I don't understand:

    _cd()
    {
    'cd' $*
    PS1="$(/usr/bin/id -unr)@$HOST:${PWD}\$ "
    }

    alias cd=cd_

    I know PS1 will customize the prompt, but what does: 'cd' $* (~mean)

    Thanks in advance

  9. #9
    oz
    oz is offline
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,733
    Hello and welcome aboard!

    This thread is going on 6 years old so I'm going to lock it, but please feel free to start a new thread of your own if you are having any problems with Linux.

    Thank you.
    oz

Posting Permissions

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