简体   繁体   中英

Why doesn't the cd command work when trying to pipe it with another command

I'm trying to use a pipeline with cd and ls like this:

ls -l | cd /home/user/someDir

but nothing happens.

And I tried:

cd /home/user/someDir | ls -l

It seems that the cd command does nothing while the ls -l will work on the current directory.

The directory that I'm trying to open is valid.

Why is that? Is it possible to pipe commands with cd / ls ? Didn't have any problem with other commands while using pipe.

cd takes no input and produces no output; as such, it does not make sense to use it in pipes (which take output from the left command and pass it as input to the right command).

Are you looking for ls -l ; cd /somewhere ls -l ; cd /somewhere ?

Another option (if you need to list the target directory) is:

cd /somewhere && ls -l

The '&&' here will prevent executing the second command (ls -l) if the target directory does not exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM