Bash – Command substitution vs I/O Redirection

bashcommand-substitutionio-redirectionshell

I am wondered about differences between I/O Redirection and Command substitution?

For example when I want to redirect which command output to cd command (I know that it's not a directory) then the cd command do not use which redirection.
For example which w3af | cd?
I have to use command substitution like cd $( which w3af ), why?

Best Answer

For the specific example you've cited, you need to use command substitution:

cd -P -- "$(dirname -- "$(which w3af)")"

for the simple reason that cd doesn't take input via STDIN. It accepts only parameters.

Moreover, which command would produce a path to a file not a directory.