With the benefit of hindsight, there are 2 distinct answers:
A: To answer the OP's question as asked with a view to understanding pipes and I/O streams:
echo . | xargs open
# Equivalent solution, using the special "$PWD" shell variable.
printf '%s\0' "$PWD" | xargs -0 open
is the most robust way to pass the current directory's path to the open
CLI via a pipe in order to have it open that directory in OSX's file-system-browser GUI application, Finder
.
Note that pwd | xargs open
is NOT robust, because it fails if the current directory path has embedded whitespace - see below.
open
requires input to be provided via command-line arguments rather than via the stdin
input stream, as is the case here (via a pipe, |
).
- Thus, in order to translate stdin input to arguments, the standard
xargs
utility is needed.
xargs
takes stdin input - coming from a pipe in this case - and invokes the command specified as its argument (open
, in this case) with the tokenized stdin input as that command's arguments.
xargs
splits by whitespace by default:
- With
.
as the input, no splitting (or interpretation by the shell) occurs, so it can just be echo
ed to xargs
.
- By contrast, the special
$PWD
shell variable, which always contains the current dir.'s full path, may contain embedded whitespace, so extra steps are needed to ensure that it's passed to open
as a single argument:
printf '%s\0' "$PWD"
prints the current dir.'s full path terminated with a NUL byte (0x0
).
- Complementarily,
xargs -0
splits stdin input into tokens by NUL - resulting in a single token in this case, preserving the value of the single input line - containing $PWD
- as is. This is safe to do, because NUL is not a legal byte in filenames.
- Note:
-0
is a nonstandard extension to the POSIX standard for xargs
, but it's implemented in both BSD xargs
(as aused on OSX) and GNU xargs
(as used on Linux). Remaining POSIX-compliant[1], the next best thing is to use line-based tokenizing with the -I
option as follows:
printf '%s' "$PWD" | xargs -I % open %
Thus, in effect the above solutions are equivalent to (what xargs
ultimately executes - see next section for an explanation):
open .
# Equivalent solution.
open "$PWD"
[1] xarg
's -I
option requires a POSIX system that is also XSI-compliant. If anyone can explain to me what that means in practical terms, I'd be grateful.
B: To provide the best answer with no constraints on techniques employed:
open .
open
expects arguments rather than stdin
input, and .
succinctly and most efficiently represents the current directory, causing open
to display the contents of the current folder in Finder
.
An equivalent, but more verbose solution is to pass the special shell variable $PWD
, which always contains the full path of the current directory (referenced in double quotes, so as to protect it from shell expansions:
open "$PWD"