Search code examples
zsh

Why zsh doesn't print string which doesn't contain new line?


I found that zsh do not print string which doesn't contain new line. Here is example:

[240 ~]>echo -n "35" > js.id
[240 ~]>cat js.id
[240 ~]>xxd -p js.id
3335

If file js.id contains new line, zsh would nomally print all string.

[240 ~]>echo "35" > js.id
[240 ~]>cat js.id
35
[240 ~]>xxd -p js.id
33350a

BTW, here is my setopt config:

[240 ~]>setopt PROMPT_CR PROMPT_SP
setopt: no such option: PROMPT_SP
[240 ~]>setopt
interactive
login
monitor
shinstdin
zle
[240 ~]>echo -n 'str'
[240 ~]>echo 'str'
str

Solution

  • This is due to prompt_cr option being set and prompt_sp option not being set.

    The sp opt is to preserve a partial line (i.e. a line that did not end with a newline) that would otherwise be covered up by the command prompt due to the PROMPT_CR option.

    Some of the built-in zsh prompts are setting cr but not sp, such as the redhat prompt

    You could workaround this issue by adding setopt prompt_sp after you configure your prompt (or unsetopt prompt_cr if you really wanted), demo:

    [user@host ~]$ autoload -Uz promptinit; promptinit; prompt redhat
    [user@host ~]$ echo -n foo > /tmp/file
    [user@host ~]$ cat /tmp/file # no output shown
    [user@host ~]$ setopt prompt_sp
    [user@host ~]$ cat /tmp/file # now it works
    foo%
    $ unsetopt prompt_cr; unsetopt prompt_sp # not recommending, just demonstrating
    [user@host ~]$ cat /tmp/file
    foo[user@host ~]$ # prompt goes to end of last output since no prompt_cr