How do I enable history to record a here-document and its input?

539 Views Asked by At

When I use the command line I can recall history by using C-P or up arrow. However this does not work when trying to recall input to a here-document.

# cat <<!
> a
> b
> c
> !
a
b
c
# cat <<! # C-p to get here, expected to see ! as last input. C-c to break out
# history 2
2053  cat <<!
2054  history 2

I'm using rxvt.

P.S. This feature works correctly when using shell within emacs

2

There are 2 best solutions below

1
On

I have the same problem. Bash 3.2.49(1)-release

psql <<EOF
SELECT * FROM blah;
EOF

My history only preserves the first line. I have got cmdhist (and lithist) set, and they work fine for other multi-line commands, but not for heredocs. Tragic.

A workaround is:

echo "
SELECT *
FROM blah
" | psql
4
On
shopt -s cmdhist

cat <<!
a
b
c
!

history | tail ...
8580  cat <<!
a
b
c
!

The cmdhist shell option, if enabled, causes the shell to attempt to save each line of a multi-line command in the same history entry, adding semicolons where necessary to preserve syntactic correctness.

The lithist shell option causes the shell to save the command with embedded newlines instead of semicolons.

From man bash