The commands that read stdin
are almost all of the filter family, i.e. programs that transform a flow of text data to a transformed one.
cat
, sed
, awk
, gzip
and even sh
are good examples of such "filters".
The cited commands, cp
, mv
and rm
are definitely not filters but commands that do things with the arguments passed, here files or directories.
The cd
command is similar to them, it expects an argument (or simulate a default one if not provided), and generally doesn't output anything on stdout
, although it might output something on it in some cases like when using CDPATH
.
Even if one want to create a cd
variant that take the target directory from stdin, it wouldn't have any effect when used in a pipeline in the Bourne shell, dash
and bash
to name a few. The last component of the command being run in a subshell, the change to a new directory won't affect the current shell. e.g.: echo /tmp | cd
would work with ksh93
but not bash
, dash
, zsh
, sh
, ...
cd <(echo /tmp)
would work with shells supporting process substitution (at least ksh
, bash
, zsh
) but wouldn't have any significant advantage compared to cd $(echo tmp)
The only use case that might be of interest would be something like:
echo tmp | (cd ; pwd)
Finally, such a variant would need to sort out the case it was given no argument but the expected behavior is to change the directory to the users's home or it was given no argument but the expected behavior is to read the name of the target directory from stdin. As there is no reliable way to decide, this is doomed.
echo 'hi' | cd /
produces no errors here with Bash on Linux (though nor does it change the current working directory, as I'm sure is explained somewhere else on the site). Which shell are you using that's giving an error? Or is this a question about what's allowed per POSIX standards? – derobert Feb 4 '16 at 22:44cd
doesn't always require an argument to change directories. Example:cd / ; pwd
and nowcd ; pwd
. No argument supplied but the directory changed. – roaima Feb 4 '16 at 22:53cd
:mv
,cp
,rm
are three examples. I'd say thatcd
is consistent by not reading from stdin. – Andrea Corbellini Feb 4 '16 at 23:08