6

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?

4

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.

  • Thank you! I wrote that which command not produce a path. :) Do you know how to check in easy way which commands take input via STDIN? – koralgooll Apr 2 '14 at 10:44
  • 3
    @koralgooll Usually the man pages would suggest that. – devnull Apr 2 '14 at 10:47
1

Here's one way to test if a command will accept stdin or not. Mind you, this is hardly exhaustive:

% { wc ; cat ; } <<INTEST
    nope
INTEST

> 1 1 5

% { cd ; cat ; } <<INTEST
    nope
INTEST

> nope

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.