Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: read -r flag not working on 5.8.1
- X-seq: zsh-workers 49262
- From: Stephane Chazelas <stephane@xxxxxxxxxxxx>
- To: Mikael Magnusson <mikachu@xxxxxxxxx>
- Cc: David Milum <david.milum.89@xxxxxxxxx>, zsh-workers@xxxxxxx
- Subject: Re: read -r flag not working on 5.8.1
- Date: Sun, 8 Aug 2021 15:04:42 +0100
- Archived-at: <https://zsh.org/workers/49262>
- In-reply-to: <CAHYJk3RGQh==kYGy4SN4LrrN7t6hDO3Ayc4YF1-7RNZLCKkZtA@mail.gmail.com>
- List-id: <zsh-workers.zsh.org>
- Mail-followup-to: Mikael Magnusson <mikachu@xxxxxxxxx>, David Milum <david.milum.89@xxxxxxxxx>, zsh-workers@xxxxxxx
- References: <CALaXwKNkYJJyfM2S+xWN15u8LA_ty7aa+r1ZFvSGNjskxk5Wnw@mail.gmail.com> <CALaXwKN_dZjtwUZvHF5fQFguOH1zkMhENDtP_zkOOaKxBm9=3Q@mail.gmail.com> <CAHYJk3RGQh==kYGy4SN4LrrN7t6hDO3Ayc4YF1-7RNZLCKkZtA@mail.gmail.com>
2021-08-08 07:59:20 +0200, Mikael Magnusson:
[...]
> >> Hello, seems like the -r (raw mode) flag which should prevent it from
> >> interpreting backslashes is not working? My understanding is the
> >> following
> >> example shouldn't clear the "CLEAR" part. I can also demonstrate with new
> >> lines.
> >>
> >> $ read -r TEST
> >> asd\c CLEAR
> >> $ echo $TEST
> >> asd
> >> $ zsh --version
> >> zsh 5.8 (x86_64-pc-linux-gnu)
>
> Your problem is not read, but echo. Try the above with echo -E $TEST
> instead and you will see the read command is fine.
[...]
More precisely, to print the contents of a scalar variable
verbatim followed by a newline character in zsh:
echo -E - $var # zsh only
print -r - $var # zsh
print -r -- $var # zsh
print -r - "$var" # zsh/ksh
printf '%s\n' "$var" # POSIX
echo -En "$var"$'\n' # zsh/bash and a few other echo
# implementations (but for bash, not if
# both the xpg_echo and posix options are
# enabled).
cat << EOF # Bourne/POSIX
$var
EOF
cat <<< "$var" # zsh and a few other shells
See also
zmodload zsh/system
syswrite -- "$var"$'\n' ||
syserror -p failed: $ERRNO
for a raw interface to the write() system call.
And btw, to read a line into a variable, the syntax is
IFS= read -r var
(in zsh or any other POSIX shell, though only zsh can read lines
containing NUL bytes).
See
https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo
https://unix.stackexchange.com/questions/209123/understanding-ifs-read-r-line
--
Stephane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author