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

P

Père Noël

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 ;-)
and the like...
 
V

Vlad Galu

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.
i'm surprised because i'm doing a lot using back quotes )))

`rsync ... ` OK
`rm -rf / ` OK ;-)
and the like...


--=20
If it's there, and you can see it, it's real.
If it's not there, and you can see it, it's virtual.
If it's there, and you can't see it, it's transparent.
If it's not there, and you can't see it, you erased it.
 
P

Patrick Plattes

Père Noël said:
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

Patrick Plattes

Père Noël said:
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

Patrick Plattes

Père Noël said:
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
 
V

Vincent Fourmond

Père Noël said:
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

Père Noël

Vincent Fourmond said:
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 )))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top