1

Creating brash one liners that work as expected take trial and error. Once I get it working, that command is the last in my history, and I can recall it !!:p.

This does not work: !!:p >> mycommands.txt

What is the correct version of the command I'm trying to execute above? Without leaving the command line, and especially without having to hit the up arrow to then copy and paste that into a new file, how do I redirect !!:p to a file?

1

1 Answer 1

1

Using echo instead of the :p modifier achieves this, but as pointed out in the comments by Kamil Maciorowski, quotes in the command are lost in this way.

To make this work with quoted command as well, use the :q modifier:

echo !!:q >> mycommands.txt

This works with commands that have spaces, single quotes, double quotes, backslashes and even newlines:

echo "Text with\\" spaces and '
Multiple Lines
'with'
"quotes"
'
echo !!:q >> mycommands.txt

Results in:

$cat mycommands.txt
echo "Text with\\" spaces and '
Multiple Lines
'with'
"quotes"
'
0

You must log in to answer this question.

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