2

I just executed the below command in the terminal:

$ while read i
> do
> echo $i
> done
4
4

But when I tried to rerun the command using up arrow history feature, I am getting only partial command like below:

$ while read i

How to print whole while loop using up arrow in KSH?

4

Line editing in the original ksh is quite limited. You may want to use one of its "clones" like zsh¹ or bash instead.

For ksh93, I find that if you use the vi editing mode (ksh -o vi) as opposed to the emacs or gmacs one, then recalling multiline commands does work, though the newline characters are rendered as ^J. That's also what you get with pdksh and its derivatives in all editing modes.

Note that you can also enter those newline characters with Ctrl+VCtrl+J for that multi-line code to be entered on one line and then be able to recall it later even in emacs mode.

Another option is to use fc to edit the previous line in a real text editor:

fc while

Would call your preferred editor (in $FCEDIT, defaults to ex on my system) on the last (possibly multiline) command line that contained while. And then run it after you save and exit in the editor.


¹ zsh also has a ksh emulation mode that makes it behave more closely to ksh in case there are some ksh behaviours you do rely on (entered with emulate ksh or when invoking zsh as ksh)

  • wow thanks a lot for your detailed explanation. set -o vi option worked for recalling the multi-line command. I do not have the option of changing the shell to zsh as I have to work on the shell which is available on the remote machine. My FCEDIT was set to ed. And I was getting the error "$ fc ksh: hist[1]: ed: not found [No such file or directory] " Thanks for your $FCEDIT suggestion, I changed it to vi and now it is working like a charm. Up-voting and accepting this as answer. Thanks again for taking time to provide the detailed explanation. It helped me a lot :) – Forever Learner Mar 28 '18 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.