Sometimes I forget my relative paths and by the time I track down where the file, is I have typed :

Cris-Mac-Book-2:weird cris$ ls ../../../
Icon?       Research    Support
Cris-Mac-Book-2:weird cris$ ls ../../../Support
Fourganizical       PicoCryptical       SupportPlan.txt
MoneyProjectical    Qwontical       Testcomms
OSICAL          StanTechStatistical todo
Cris-Mac-Book-2:weird cris$ ls ../../../Support/PicoCryptical
S   cs  mini    php py  readme
Cris-Mac-Book-2:weird cris$ ls ../../../Support/PicoCryptical/py/StanTechPico.py

To now run this script, I find myself pressing until I get back to the start of the command, then replacing python with ls.

When I do this long-hold (which seems to be often) I am always thinking, there must be a faster way to edit the command string, or execute the previous command's output.

So, not really qualifying as a bash superuser, I tried to pipe the output of ls to python, which didn't work as intended :

Cris-Mac-Book-2:weird cris$ ls ../../../Support/PicoCryptical/py/StanTechPico.py | python
  File "<stdin>", line 1
SyntaxError: invalid syntax
Cris-Mac-Book-2:weird cris$

So my question : how to put the STDOUT output of a previous command, in the argument position of a new command?

  • 4
    Have you tried using the Home key?
    – Kruug
    Feb 26, 2013 at 17:57
  • You know my meaning, to get to the start of the command quicker, but the Fn ← doesn't work from my Terminal. It produces a seemingly random command from history. Feb 26, 2013 at 18:01
  • I guess I was unaware that Macs didn't have a dedicated Home key. I apologize.
    – Kruug
    Feb 26, 2013 at 18:52
  • It's ok. I almost feel like it's Mac's fault. They designed out the Home key. Damn them. Feb 26, 2013 at 18:57

6 Answers 6


^ls^python will perform your immediate need (repeat previous command, substituting python for the ls)

  • 2
    That is awesome! Feb 26, 2013 at 17:58
  • @CrisStringfellow You are right, it is awesome!! I had no idea it was possible to do something like this.
    – BenjiWiebe
    Feb 26, 2013 at 18:06
  • 2
    I find python !$ a bit easier to type if it's just to reuse the last argument. The ^foo^bar syntax is nice if you made a typo and want to keep most of the command except for a small replacement (wherever it is) Feb 26, 2013 at 22:14

I'm not certain you're actually asking

But I guess what I really want is to know how to put the STDOUT output of one command in the argument position of another command, from the command line?

I think what you might find useful is using $_

$ ls /tmp/long/path/which/contains/file
$ python $_

The $_ is replaced with the final argument of the previous command you executed.

See http://tldp.org/LDP/abs/html/internalvariables.html

However, if you really do want to use the stdout as an argument for another command, you can use xargs for that (see http://linux.die.net/man/1/xargs)

$ ls /tmp/long/path/which/contains/file | xargs python
  • That's awesome, and also answers it. The question is unclear and messy I will clean it up. Feb 26, 2013 at 18:04
  • 1
    @CrisStringfellow Your question is really bringing some interesting answers.
    – BenjiWiebe
    Feb 26, 2013 at 18:07
  • Yes, anything to make bashing (is that the verb?), more efficient! Feb 26, 2013 at 18:10

Have you ever used backticks?

python `ls ../../../somefile`

Will call python with the output of ls as its argument.

Not totally sure if that is what you are asking, though.

  • Ah that's good. Except I want that same effect without having to cursor back to the start of the command and insert python before ls. Feb 26, 2013 at 17:53
  • 1
    Readline key bindings recall the previous command, delete the first word, and place the insertion point ready to add 'python' using 5 keystrokes without taking your fingers from the home keys: Ctrl-p-a-[ d. Ctrl-p previous-line Ctrl-a start-of-line Ctrl-[ Escape Esc-d kill-word. Esc is a substitute for Meta, Ctrl-[ is a substitute for Esc. Type this enough and it comes out faster than you can think...
    – idoimaging
    Feb 26, 2013 at 18:23
  • @AndrewCrabb Jeezuz I just tried that and it was awesome. Feb 26, 2013 at 18:56

You can type


and press ALT + .

Pressing ALT + . inserts last argument of the last command. Pressing the shortcut again goes further backwards.

EDIT: I have tested this on different Linux and OpenBSD using bash,sh,ksh,dash,zsh. It does not seams work in (t)csh. Which surprises me because I thought its a feature of readline?!

  • That's a cool one, too. On Mac I get this character, ≥, and not the last command. Is there any way to give a Mac terminal these bindings? Feb 26, 2013 at 21:17
  • @tim you should precise where that shortcut works… @cris in shell, !$ expands to the last argument of the previous command, so you want python !$ Feb 26, 2013 at 22:10
  • @DamienPollet Great, so I think I have !$ (last argument of previous command), $_ (last output of previous command). This is awesome! Feb 26, 2013 at 22:23

Other than Andrew's answer, a good trick to know is that Ctrl+a jumps the cursor to the start of the line.


Your immediate need seems solved, but here are some solutions for your long term need of finding "a faster way to edit the command string".

From your example, it looks like you already have bash completion (http://www.hypexr.org/bash_tutorial.php#completion) on your system, so you should be able to do the exact same thing with Tab, just start the command with python instead of ls then Tab away.

Here are some of the basic shortcuts I find myself using all the time (most work on any *nix based system, but some ... especially OS X ... can be cranky sometimes).

  • Ctrl + or Ctrl + jumps by "words" instead of individual characters.
  • Ctrl + A jumps to the beginning of the command line
  • Ctrl + E jumps to the end of the command line
  • !! will repeat the last command in the history (and print out what the command was)

    $ ls
    $ !!

  • !$ will print the very last argument from your previous command ($_ is mentioned above, which is bash specific, is basically the equivalent ... there, I've done it, let the debate begin)

    $ ls ../file.py
    $ python !$
    python ../file.py
    script output ...

For a few more great ones, see the "Keyboard Shortcuts" section on Linux Terminal Command Reference. Even though that page is specific to Linux Mint, the only *nix system I find unhappy with those shortcuts is OS X.

  • ! Tab, yes, I had forgotten how to get completion. This is excellent. Or will be once I 'integrate it into my workflow and thinking'. There are helpful. I wonder what debate you have started! Any idea why OS X term gives me beep (or in my case screen flash, since I hate the beeps) the first time I strike Tab, then it cows and yields to the 'completion' the next Tab, then back to beeping the next time, and so on? Feb 27, 2013 at 0:00

You must log in to answer this question.

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