1

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.

Ibrahim Quraish
  • 3,889
  • 2
  • 31
  • 39
  • `all the arguments (part of argument list)` - that's not clear to. Do you want all arguments or part of argument list? In `ls -l file` command `-l file` are the arguments in `Bash` understanding. – Arkadiusz Drabczyk Aug 09 '19 at 10:55
  • Ah, edited and removed that portion "(part of argument list)". Sorry for the confusion. It is all the arguments only – Ibrahim Quraish Aug 09 '19 at 11:09
  • *all the arguments* but if you ran `ls -l f1 f2 f3` you want to get just `f1 f2 f3` and not `-l f1 f2 f3` right? – Arkadiusz Drabczyk Aug 09 '19 at 11:11
  • `-l` *is* an argument. The shell doesn't know to treat it differently than any other argument, and `ls` only treats it differently because of the leading `-`. – chepner Aug 09 '19 at 13:54

2 Answers2

1

Try leaving out the second !:

$ ls foo bar baz
foo bar baz
$ echo !ls:*
echo foo bar baz
foo bar baz
Jon
  • 3,573
  • 2
  • 17
  • 24
1

!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:*
fra
  • 832
  • 6
  • 14