There are a few ways to quickly change the first word in a bash command. One way is to use the alias feature of bash. Another way is to use the function command.
If your workflow is populated with a lot of repetitive actions, then it never hurts to look for ways to improve and streamline your workflow. Today’s SuperUser Q&A post has some helpful suggestions for a reader seeking to improve his workflow.
Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.
Screenshot courtesy of Matt Joyce (Flickr).
The Question
SuperUser reader coin wants to know how to quickly change the first word in a bash command:
Is there a quicker way for coin to change the first word in bash commands?
Some Examples
I know that I can hit Ctrl+a then Del to remove the first word, but I am wondering if there is a quicker way to do it.
The Answer
SuperUser contributors Spiff, Hastur, jjlin. and Gustavo Giraldez have the answer for us. First up, Spiff:
Followed by the answer from Hastur:
Or
Your examples happened to only repeat the last word, so !$ worked fine. If you actually had a lot of arguments that you wanted to repeat, and you just wanted to change the first word, you could use !*, which expands to all words of the previous command except the zeroth.
See the “HISTORY EXPANSION” section of the bash man page. There is a lot of flexibility there.
Then the answer from jjlin:
So if you write the last command with a blank space at the beginning, then this command will probably not finish from the history. When you execute your new command with !$, the shell will not take the parameters from the last command line typed, but only from the last part of the history.
Here are some more helpful words and commands.
And our final answer from Gustavo Giraldez:
Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.