12

This is macOS specific, but seems too unixy to go in the Ask Different community.

In Terminal, I can pwd, copy the result, and type open and paste the result and the folder will open in the Finder, but

pwd | open

prints the help documentation for open. Why doesn't piping work but pasting does?

3
  • that will only work if open expects you to type the directory name after you press enter.
    – Jasen
    Commented Jul 23, 2016 at 4:32
  • 9
    Also, this should (not a mac user) work: open . (for the current directory)
    – user137998
    Commented Jul 23, 2016 at 10:44
  • @BharadwajRaju open . works and does seem the easiest. ty.
    – Chuck
    Commented Jul 23, 2016 at 13:00

5 Answers 5

28

The open utility on macOS does not read from standard input, but take its arguments from the command line.

To open the current working directory, you would have to say

$ open "$( pwd )"

or

$ open "$PWD"

or simply

$ open .

as pointed out in the comments below.

With the -f flag, open can be made to read from standard input, but only to open whatever it receives in the default text editor.

6
  • 1
    If the shell is (t)csh, you'd want open $cwd:q instead. And open (pwd) /open $PWD if it's fish Commented Jul 23, 2016 at 7:46
  • 1
    @StéphaneChazelas I believe the default shell on Darwin is bash, but it's always good to see solutions for other shells too.
    – Kusalananda
    Commented Jul 23, 2016 at 7:55
  • For completeness, with rc, es, akanga shells: open `pwd or open `{pwd} Commented Jul 23, 2016 at 10:19
  • 5
    Or you can use the syntax- open . for the current working directory or the parent directory open .. or up two directories open ../.. and so forth.
    – fd0
    Commented Jul 23, 2016 at 10:46
  • @StéphaneChazelas: Why would $cwd:q be preferable to "$cwd" in a csh-related shell? Commented Jul 24, 2016 at 2:18
19

I don't have a Mac so I can't test it, but the solution should be something like:

open "`pwd`"

Not all programs take their input from stdin which would be necessary for the pipe to work.

10
  • 1
    The double quotes are unnecessary.
    – tbodt
    Commented Jul 23, 2016 at 18:50
  • 4
    @tbodt: Try it in /var/tbodt's files. Commented Jul 23, 2016 at 19:00
  • 2
    @tbodt: How would any shell know if the space used is to separate arguments or part of an argument? Commented Jul 23, 2016 at 19:06
  • 2
    @jamesdlin: Go complain on the bash development project, but don't forget to also go bash all older shells and most newer shells while you're at it. Over and out! Commented Jul 23, 2016 at 19:34
  • 1
    @JuliePelletier I'm just saying that you claim it's impossible for any shell, but shells such as fish behave exactly as tbodt describes.
    – jamesdlin
    Commented Jul 23, 2016 at 19:35
10
pwd | xargs open

xargs shoves its standart input into the arguments of its first argument.

2
  • 2
    That's only if the current working directory path doesn't contain blanks or newline or single quotes or double quotes or backslashes. Commented Jul 24, 2016 at 4:06
  • pwd | xargs -d '\n' open
    – belkka
    Commented Mar 17, 2019 at 4:23
9

The other answers are totally correct. If you want an easy shorthand, you can do as @fd0 proposed, and just use

open .

to open the current directory. The current directory is named . (a single dot) in Unix, the parent directory .. (two dots).

0

You can use a pipeline with -f to open the content of the previous command in a Text Editor. This just applies for text editors. For other apps you'll need to use the suggestion in the other questions.

For example:

ls | open -f

This would open your default text editor with the ls output content.

You can also specify the text editor to use using -a:

ls | open -a 'Atom' -f

This would open Atom with the ls output content.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .