i am trying to move to the dest_dir from Current Working Directory using the following script. my intension is instead of using cd dest_dir, i have to use the script to do the job.

when ever i tried with the follwing script, iam again in my Current working directoy...

#!/usr/bin/bash
CWD=pwd
cd ${CWD}/dest_dir

Regards,
eapln

Recommended Answers

All 4 Replies

That's because bash tells the computer to spawn a new instance of bash. So, the script makes a new copy of bash, changes into the desired directory, and then discards the new shell (bash). Once it discards the new shell, you are back at the old shell, which is in the same directory it was in when the new shell was run. Unfortunately, there is no way to make this work without doing it on the command line (ie: from within the script alone). You can however, make it work by first typing source and then the script you want to run. So, something like source /scripts/mynewpath will work (because of "source").
Personally, what I would do.... is simply make a bash function, and have it created in like .bashrc or some such. Just for an example, try this at this command line (don't worry if the prompt becomes a >, it's supposed to):

newpath() {
cd $1
}

Once you are back at your normal prompt... make sure you are in the home directory or something, and type newpath / you should now be at the root directory. This gives you the functionality you want, without the need of +x files laying around.

thank you comatose..
now i am able to move to dest directory.
but newpath / gives the error like "command not found"

Regards
eapln

You have to first make the function in bash... like I did above. Once you make the new function, then you can do "newpath /". Also, you have to make sure you make the function each time you load a new terminal or whatever (or make sure the startup files for those programs have that in it)

thank you.
it works now. thanks once again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.