Dir.chdir("..") versus `cd ..`

i discovered this wornig that :

cd .. as no effect but :

Dir.chdir("…")

as the right effect, why isn’t the first not working ?

i’m surprised because i’m doing a lot using back quotes )))

rsync ... OK
rm -rf / OK :wink:
and the like…

On 12/11/06, Père Noël [email protected] wrote:

i discovered this wornig that :

cd .. as no effect but :

Dir.chdir(“…”)

as the right effect, why isn’t the first not working ?

Because “cd …” executes in a child process. So the cwd change only
takes place in it.

Père Noël schrieb:

i discovered this wornig that :

cd .. as no effect but :

Dir.chdir("…")

as the right effect, why isn’t the first not working ?

maybe the first one creates a new environment and close it after the
last backquotes, so you’ll change the directory, but only in this
created environment.

Dir.chdir changes the directory in the current environment.

correct me, if I’m wrong,
patrick

Père Noël schrieb:

i discovered this wornig that :

cd .. as no effect but :

Dir.chdir("…")

as the right effect, why isn’t the first not working ?

maybe the first one creates a new environment and close it after the
last backquotes, so you’ll change the directory, but only in this
created environment.

Dir.chdir changes the directory in the current environment.

correct me, if I’m wrong,
patrick

Père Noël wrote:

i discovered this wornig that :

cd .. as no effect but :

Dir.chdir("…")

as the right effect, why isn’t the first not working ?

i’m surprised because i’m doing a lot using back quotes )))

When you’re using backquotes, Ruby forks a shell and feeds it with the
command line given. With cd .., you do change the current working
directory… for the forked shell. Not for the Ruby program… And the
forked shell dies immediately after the command is finished, so no way
of doing so. However, if you need to run only one command in another
directory, you might try:

cd ..; a_nice_command

Cheers,

Vince

Père Noël schrieb:

i discovered this wornig that :

cd .. as no effect but :

Dir.chdir("…")

as the right effect, why isn’t the first not working ?

maybe the first one creates a new environment and close it after the
last backquotes, so you’ll change the directory, but only in this
created environment.

Dir.chdir changes the directory in the current environment.

correct me, if I’m wrong,
patrick

Vincent F. [email protected] wrote:

When you’re using backquotes, Ruby forks a shell and feeds it with the
command line given. With cd .., you do change the current working
directory… for the forked shell. Not for the Ruby program… And the
forked shell dies immediately after the command is finished, so no way
of doing so. However, if you need to run only one command in another
directory, you might try:

cd ..; a_nice_command

yes, thanks for all (and to all answers) it’s clear enough to me right
now )))