Re:Unix tricks
by pjungwir on Thursday March 07, 2013 @12:52PM
(#5339013)
Another nice ending is `:p` to print the command instead of executing it. I use this if I'm doing something complicated and I want to make sure it's right. Or if I'm saying `!-n:foo` with n>2. Then just up-arrow and enter to run it for real.
[ Parent
| on HN ]
Here is a detail when using '!!:n' with ':p' when trying to iteratively construct a complex command. I want to emphasize the use of the up-arrow (which will show the interpolated arguments), as opposed to rewriting exactly what you wrote in the previous command, since the usage of ':p' will be interpreted by the shell as a command in itself:
$ echo a b c d
a b c d
$ echo !!:2:p
echo b
$ echo !!:2
-bash: :2: bad word specifier # there was only 1 argument in last command
However: $ echo a b c d
a b c d
$ echo !!:2:p
echo b
$
<up-arrow pressed once will give the following prompt>
$ echo bI use magic-space for that in my inputrc:
$if Bash
Space: magic-space
$endif
Basically does the same thing as :p, but after a space instead of enter.Edit: fixed formatting.
shopt -s histverify
to show the expanded command before executing it. Then just enter. I never get these right the first time.