eshell-add-command-to-history to minibuffer-exit-hook, and remove it
after read-from-minibuffer returns.
(unwind-protect
(let ((eshell-non-interactive-p t))
(add-hook 'minibuffer-setup-hook 'eshell-mode)
+ (add-hook 'minibuffer-exit-hook 'eshell-add-command-to-history)
(add-hook 'eshell-mode-hook 'eshell-return-exits-minibuffer)
(unless command
(setq command (read-from-minibuffer "Emacs shell command: "))))
(remove-hook 'eshell-mode-hook 'eshell-return-exits-minibuffer)
+ (remove-hook 'minibuffer-exit-hook 'eshell-add-command-to-history)
(remove-hook 'minibuffer-setup-hook 'eshell-mode))
(unless command
(error "No command specified!"))