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.
- 10-04-2005 #1Just 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.
- 10-04-2005 #2Linux Engineer
- Join Date
- Mar 2005
- Posts
- 1,431
Run the file like this:
instead of like this:Code:. scriptname
I think it will work.Code:./scriptname
- 10-04-2005 #3Just 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.
- 10-05-2005 #4Linux 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".
- 10-05-2005 #5Just Joined!
- Join Date
- Feb 2005
- Posts
- 46
Good idea. I didn't think about that.
Thank you so much!
- 10-09-2005 #6Linux 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_
- 01-22-2007 #7Just Joined!
- Join Date
- Jan 2007
- Posts
- 1
the source code !!
can u post the code for changing the cwd to some other
in bash /csh environment !
Originally Posted by helpmhost
- 06-23-2011 #8Just 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
- 06-23-2011 #9forum.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



