Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

2 Answers

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 "\$@"
}
EOF
}
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? –  REACHUS Mar 12 '13 at 22:09
add comment

The closes strategy I know, is Alt+.

This allows you to cycle through all previous arguments.

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

Your Answer

 
discard

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.