2

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

potong
  • 55,640
  • 6
  • 51
  • 83
  • You're right. The last command on the commandline was; "cat << !", thus, your can't recall the input to the 'program' cat. What are you trying to achieve? – Ezeyme Nov 24 '11 at 14:27
  • I have what sounds like the same problem. I was trying to pipe some SQL into a database client using here docs (psql < – Jonathan Hartley Aug 08 '13 at 12:53

2 Answers2

2

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
Jonathan Hartley
  • 15,462
  • 9
  • 79
  • 80
  • As @mr.spuratic notes in a comment below, this seems to have been a bug that was fixed around Bash 4.2. Certainly it works somewhat better for me in Bash 4.2.46(2)-release. (Although not perfectly - it adds two newlines between each line of the heredoc, instead of just one.) – Jonathan Hartley Nov 29 '17 at 17:09
2
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

  • 1
    `cmdhist` is already on - so no joy there. `lithist` was set off so I enabled it but again no success. Thanks for the effort. – potong Dec 04 '11 at 14:48
  • @user1077830: What shell are you using then? This doesn't work as you show in Bash 3.2.49. – Jonathan Hartley Aug 23 '13 at 13:55
  • 1
    I believe this was changed in 4.1 dev, and should work from 4.2. See Changelog line 9666 in bash-4.2 source. `cmdhist` should be set to keep all lines in one history entry, as described. – mr.spuratic Oct 29 '14 at 17:05
  • I came back to this question years later, and you are right - it now works for me in Bash 4.2 (although not perfectly - my history inserts a second newline between each line of the heredoc. Irksome but better than it was) – Jonathan Hartley Nov 29 '17 at 17:05