Ibrahim Quraish
Ibrahim Quraish

Reputation: 4095

Recall all arguments from a previously executed command in bash shell

This link gives the pointer on how to recall the arguments of the last successfully executed command in the command line.

I wanted to access all the arguments of a particular command from the shell history.

To me only this syntax works

ls f1 f2 f3
file !ls:1-3 --> file f1 f2 f3

And, if I use !* which should give all the arguments of previous command throws error

file !ls:!*
-bash: !: unrecognized history modifier

I can only use this syntax (i.e) all arguments of last executed command.

file !*

Problem with the above methods are if I had executed ls with some option, for eg: ls -l, then the file command would have thrown a different output as the option of ls would be considered as first argument in this case.

Upvotes: 1

Views: 124

Answers (2)

fra
fra

Reputation: 852

!ls returns the last ls command. If you want the second to last or even an older ls execution you can use history to retrieve the command you want

history | grep ls

355  ls foo bar baz
446  ls -a
447  ls -ah

And then use @Jon solution or yours to get the arguemnts

echo !355:*

Upvotes: 1

Jon
Jon

Reputation: 3671

Try leaving out the second !:

$ ls foo bar baz
foo bar baz
$ echo !ls:*
echo foo bar baz
foo bar baz

Upvotes: 1

Related Questions