LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   command " cd .. " not working in bash script? (https://www.linuxquestions.org/questions/linux-newbie-8/command-cd-not-working-in-bash-script-794880/)

Kilam orez 03-12-2010 12:37 AM

command " cd .. " not working in bash script?
 
Hi dear linux users.

i wonder why the cd ./.. or cd ./../ or
cd .. command does not work, when used in bash script.

But it works fine, when used on the command line.

can i anybody tell me, why is it so? sorry if my question is silly.

With Regards,
kilam

Galib 03-12-2010 12:44 AM

EDIT: Mis-Read your question.

evo2 03-12-2010 12:52 AM

What do you mean "does not work?". What happens? Is there an error?

Can you make the script output pwd before and after tring to cd?

Cheers,

Evo2.

Kilam orez 03-12-2010 02:42 AM

Quote:

Originally Posted by evo2 (Post 3895325)
What do you mean "does not work?". What happens? Is there an error?

Can you make the script output pwd before and after tring to cd?

Cheers,

Evo2.

hi,

my script(fc_mov_2.sh) is:----

#!/bin/bash

cd ./../

#----end---------


when i run it, i get:---

hg@hg:~/Desktop/is64/trial> ./fc_mov_2.sh
hg@hg:~/Desktop/is64/trial> pwd
/home/hg/Desktop/is64/trial


No error is shown. i wonder, what is happening?

regards,
kilam

evo2 03-12-2010 02:48 AM

The script is a separate process. To illustrate (as I suggeted in my first post), use the following script:

Code:

#!/bin/bash
pwd
cd ..
pwd

The script can't change the working dir of the shell that calls it. If you really want to do something like that you need to source the script. First, remove the "#!/bin/bash" from the first line and then do

Code:

. fc_mov_2.sh
Cheers,

Evo2.

catkin 03-12-2010 02:58 AM

Quote:

Originally Posted by evo2 (Post 3895449)
First, remove the "#!/bin/bash" from the first line

Wouldn't do any harm but not necessary.

evo2 03-12-2010 03:30 AM

Quote:

Originally Posted by catkin (Post 3895454)
Wouldn't do any harm but not necessary.

Good point. I guess it's just a convention I use so that when I look at a script I immediately know whether it should be sourced or executed.

Cheers,

Evo2.

Kilam orez 03-12-2010 04:14 AM

Quote:

Originally Posted by evo2 (Post 3895449)
The script is a separate process. To illustrate (as I suggeted in my first post), use the following script:

Code:

#!/bin/bash
pwd
cd ..
pwd

The script can't change the working dir of the shell that calls it. If you really want to do something like that you need to source the script. First, remove the "#!/bin/bash" from the first line and then do

Code:

. fc_mov_2.sh
Cheers,

Evo2.

Thank you Evo2, it worked !!!!


All times are GMT -5. The time now is 02:04 AM.