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.

4
  • 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. Commented Aug 9, 2019 at 10:55
  • Ah, edited and removed that portion "(part of argument list)". Sorry for the confusion. It is all the arguments only Commented Aug 9, 2019 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? Commented Aug 9, 2019 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
    Commented Aug 9, 2019 at 13:54

2 Answers 2

1

Try leaving out the second !:

$ ls foo bar baz
foo bar baz
$ echo !ls:*
echo foo bar baz
foo bar baz
1
  • I'm not sure how this answers OP's question. If you used ls -l as OP did you would get -l in the output and OP doesn't want that. Commented Aug 9, 2019 at 11:04
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:*
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.