Command Line – Piping pwd to Executable as Argument

command line

So I have an alias for "sublime text" http://www.sublimetext.com/docs/2/osx_command_line.html
and very often I need to open a file or a folder right from the terminal, easy for a file:

subl myfile.html

and for a folder

subl /Users/me

But how can I use 'pwd' command to open current directory in sublime? Is there a way to pipe it in?

Best Answer

$(pwd)

or, you can use backticks, like

`pwd`

So:

subl `pwd`

Either way, what happens is the command pwd gets executed, then its return text gets passed as the command-line. A good way to see what's happening, assuming you're using bash, is to issue set -x then run your command. As this example, where subl is aliased to echo shows, the result of pwd is passed on the command-line to the expanded alias (lines beginning with + indicate the actual command being executed):

$ alias subl=echo
$ mkdir /tmp/abc
$ cd /tmp/abc
$ set -x
$ subl `pwd`
++ pwd
+ echo /tmp/abc
/tmp/abc
$

(When you're done, you can stop printing commands by issuing set +x.)

Related Question