Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 3 down vote accepted

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 .
share|improve this answer
Thank you. Just out of curiosity, how long have you been using UNIX/Linux? – ΔλЛ Mar 12 '13 at 22:09

The closes strategy I know, is Alt+.

This allows you to cycle through all previous arguments.

share|improve this answer
This assume emacs keybindings (default). – jordanm Mar 12 '13 at 18:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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