tcsh-org / tcsh

This is a read-only mirror of the tcsh code repository.
https://www.tcsh.org/
Other
242 stars 41 forks source link

How to read a file into a variable preserving newline #108

Open edwinjhlee opened 5 months ago

edwinjhlee commented 5 months ago

Fail using command substitution

cat >a.txt <<A
a
b
c
A
set data="`cat a.txt`"
cat -n <<A
$data
A

# The output is 
#  1    a b c

setenv data "`cat a.txt`"
cat -n <<A
$data
A

I read the mac document, it seemed csh just turn it into a word list during command substitution. I have tried using unset csubstnonl. It doesn't work.

My questions is, is there another way in csh that I can read the file and save the content to a variable preserving newline ?

My csh version is :

tcsh 6.24.10 (Astron) 2023-04-14 (aarch64-apple-darwin) options wide,nls,dl,al,kan,sm,rh,color,filec

Krush206 commented 5 months ago

I believe this is only possible from literals.

set a = 'a\
b\
c'

I've an ongoing work implementing variable assignments from pipes and redirections. Non-indexed assignments behave the same as set a = "`cat`" < myfile. Indexed assignments (e.g: set a[1] = 'a b c') preserve newlines. It may be used to store binary data, but is not guaranteed to preserve all bytes (similarly to Bourne-compatible shells).