I want other developers to have some convenient methods. Therefore I want to give them a script, that automatically sets an alias.

E.g the command grunt compileModule:[modulename] should set the alias cprev to be this exact command. The command should still be executed after setting the alias!

As we are commonly working multiple days on the same module, the alias should also get an entry in the ~/.bashrc file (this is the main 'feature', so whenever they close the bash, they dont need to put in the whole command on reentering)

Now there are many posts about historyand how to look up which commands where run last, etc., but I could not find out how to sort of 'concatenate' all the information to result in what I need.

I was thinking creating an alias for grunt.

Something like

alias grunt='~/myScriptForAliasHistory.sh;grunt $1';

would that be suitable?


to make this more clear:

The original command is

grunt compileModule:[name]

and it should be working like this after the modification.

It should only do an additional step, so you can call the same function as


as well (with the same parameters)

The workflow could look like this:

  1. Developer starts working on a new module. In order to compile it, he writes grunt compileModule:[name].
  2. The same developer needs to restart his bash/pc/console. Now he does not want to write the whole command again, but wants to only type cprev (which then executes grunt compileModule:[name])
  3. The next day he starts working on module [anothername], so he executes grunt compileModule:[anothername]
  4. If he now restarts, the command cprev will execute grunt compileModule:[anothername]

In other words: Whenever grunt compileModule is called, it should overwrite the alias cprev with its own current call parameters.

  • What should be executed when you run your alias? Should it be a recursive call (i.e. should the alias invoke itself)?
    – Kusalananda
    Dec 6, 2017 at 9:55
  • You can't use positional parameters (e.g. $1) with an alias. You'd have to generate a function instead.
    – B Layer
    Dec 6, 2017 at 10:06
  • 1
    @Kusalananda I added an example workflow Dec 6, 2017 at 10:10
  • @BLayer I know, it was just a thought of mine. a pseudo code Dec 6, 2017 at 10:10

1 Answer 1


Using history expansion with ! will do this without an alias or function:

$ grunt compileModule:[something]

$ # other things

$ !grunt

The last !grunt will cause the shell to look up the last command in the command line history that starts with the string grunt and run that.

In bash:


Refer to the most recent command preceding the current position in the history list starting with string.

If the shell has persistent history (usually enabled by setting HISTFILE), then this will also work across consecutive shell sessions, and depending on the shell, it may even work in concurrent sessions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .