Assuming I have a script for grepping through a bunch of files with certain options and run this script with many different words:

$ mygrep worda
$ mygrep wordb
$ mygrep wordc

I know !$ is a placeholder for the last word of the last command. Is there a similar placeholder for the first word so I could save myself some typing?


Not $! but !$ -- bash history expansions begin with the !.

The opposite of !$, the last argument, is !^, the first argument.

The previous command can be retrieved as !:0, not quite as easy to type as !$.

You can also do a search and replace with ^worda^wordb. That is not global search&replace, it only replaces the first one found. Global search and replace is !!:gs/worda/wordb.

Phew. More info at https://www.gnu.org/software/bash/manual/bashref.html#History-Interaction

  • 1
    Three letters are two too many. Now I think it'll be more convenient to do something like alias g=mygrep, and then after I'm done with my searches do unalias g (or just log out.) – muk.li Oct 15 '15 at 21:40
  • 3
    You could also use Up-Arrow followed by Ctrl-W to erase previous word (i.e. the last argument). – cas Oct 15 '15 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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