Issue
Is there any way to save the command into bash history as the multiline command and then to navigate up and down to make some edits and run it again?
$ shopt -s cmdhist
$ shopt -s lithist
$ echo "1"\
&& echo "2"\
&& echo "3"
Now, if I hit the up-arrow I'd like to see the
$ echo "1"\
&& echo "2"\
&& echo "3"
..again, but bash gives me the echo "1"&& echo "2"&& echo "3"
. I.e. it removed the new lines although the cmdhist
and lithist
flags are set.
If the up-arrow worked and displayed the multi-line command, how can I navigate up and down in it?
Solution
It's not possible in Bash
as \
before a newline is ignored in Bash
. From man bash
:
A non-quoted backslash () is the escape character. It preserves the literal value of the next character that follows, with the exception of . If a \ pair appears, and the backslash is not itself quoted, the \ is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).
If you're willing to change your shell or want to experiment check out zsh
, it can do what you're looking for:
$ echo a \
echo b
Press C-p and see that newlines are retained:
$ echo a \
echo b
Answered By - Arkadiusz Drabczyk Answer Checked By - Marilyn (WPSolving Volunteer)