I would like to configure bash so that when I execute command (preferably from a list of commands, not any command) without an argument, it takes the argument of previous command.

So, for example I type emacs ~/.bashrc, then when I enter source, bash executes source ~/.bashrc. I know it's possible but I don't know where to look for such options.


You can press Space then Meta+. before pressing Enter. This has the advantage that you can use it even with commands that make sense when applied to no argument. For source, use . to type less.

If you're old-school, you can use !^ instead to recall the first argument from the previous command, or !$ to recall the last argument, or !* to recall all of them (except the command name).

You can get exactly the behavior you describe by writing functions that wrap around each command. The last argument from the previous command is available in the special parameter $_.

make_wrapper_to_recall_last_argument () {
  for f do eval <<EOF; done
function $f {
  if [ \$# -eq 0 ]; then set -- "\$_"; fi
  command $f "\$@"
make_wrapper_to_recall_last_argument source .
  • Thank you. Just out of curiosity, how long have you been using UNIX/Linux? – syntagma Mar 12 '13 at 22:09

The closes strategy I know, is Alt+.

This allows you to cycle through all previous arguments.

  • 1
    This assume emacs keybindings (default). – jordanm Mar 12 '13 at 18:07

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.