4

I have a problem with redirections :

$ which python3

gives me

/Library/Frameworks/Python.framework/Versions/3.7/bin/python3

and

$ ls -l /Library/Frameworks/Python.framework/Versions/3.7/bin/python3

gives me

lrwxr-xr-x  1 root  admin  9  5 fév 18:30 /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -> python3.7

but

which python3 | ls -l

don't gives me the same result.

Do you know why ? And what is the right command for redirection ?

I'm using OSX.

I have to say that the following question pass the output of previous command to next as an argument may be the same as this one, but if I look the answers that were given, there I'm lost. To be useful, they require more advanced knowledge or study than those given here.

1
  • I've updated the list of duplicates to some other better (IMHO) questions on the same topic. Especially Redirecting the content of a file to the command "echo" mirrors your question quite well as echo behaves like ls with regards to (not) reading its standard input. Commented Feb 16, 2019 at 10:33

3 Answers 3

13

ls does not take input from standard in, but only from arguments:

Try ls -l "$(which python3)"

12

ls does not read from the pipe. In fact, ls does not use its standard input at all.

Instead, you have to pass the thing you'd like to run ls -l on via the command line of ls:

ls -l "$( which python3 )"

This uses a command substitution on the command line of ls -l which will expand to the output of the which command. This will then be used as a command line argument for ls.

Alternatively:

ls -l "$( command -v python3 )"

Related:

8

Other answers are good, but this is also handy:

which python3 | xargs ls -l

xargs gets values from stdin and appends them as command-line argument to the specified program.

2
  • This is much the best answer in my view, easy to apply to similar and more general cases of command line usage and output redirection. Thanks @jick. Commented Jan 15, 2020 at 10:49
  • This answer doesn't behave weirdly if you're passing quotes " in args, +1 Commented Nov 26, 2021 at 1:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.