Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: read -r flag not working on 5.8.1



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