bash - Shell script strange echo behavior -
i want print content have obtained split of array in way :
string="abc test;abctest.it" ifs=';' read -a array <<< "$string" name="${array[0]}" url="${array[1]}" echo -ne "\n$url,$name" >> "$outputdir/$filename" but output file doesn't contain url part
i think problem "." don't know how fix it
if try
echo $url it's work!
thanks
i've tried printf , hardcoded filename nothing!
printf '%s %s\n' "$url" "$name" >> test.txt it seems when try concatenate thing after variable $url part of variable deleted or overwritten output file
for example if try
printf '%s %s\n' "$url" "pp" >> test.txt what simple cat test.txt :
pptest.it but content of variable $url must abctest.it
it's strange
to complement chepner's helpful answer:
if output doesn't look expect like, it's worth examining contents hidden control characters may change data's appearance on output.
\r, cr (carriage return; ascii value 13) notorious example 2 reasons:- (as @chepner has stated) moves cursor beginning of line mid-string, erasing @ least part of came before it; e.g.:
echo $'abcd\refg'printsefgd:\rcauses after restart printing @ beginning of line,dstring before\rsurviving, because happened be 1 char. longer string came after.
(note:$'...'syntax so-called ansi c-quoted string, allows use of escape sequences such\rin$'...\r...'create actual control characters.)
- files unexpected
\rchars. occur when interfacing windows world, line breaks aren't\nchars.,\r\nsequences, , such files behave strangely in unix world.
- (as @chepner has stated) moves cursor beginning of line mid-string, erasing @ least part of came before it; e.g.:
- a simple way examine data pipe
cat -et, highlights control characters^<char>sequences:^mrepresents\r(cr)^irepresents\t(tab. char)^[represents esc char.... # see 'man cat'- the end of line represented
$ - thus, file windows-style line endings show
^m$@ end of lines outputcat -et.
cat -etapplied above example yields following, makes easy diagnose problem:echo $'abcd\refg' | cat -et # -> 'abcd^mefg$' - note ^m
dos2unixgo-to tool converting windows-style line endings (\r\n) unix ones (\r\n), tool doesn't come preinstalled on unix-like platforms, , it's easy use standard posix utilities perform such conversion:awk 'sub("\r$", "")+1' win.txt > unix.txt- note posix-compliant command doesn't allow replace file in-place, however:
- if have gnu
sed, following perform conversion in place:sed -i 's/\r$//' winin_unixout.txt
- ditto bsd
sed(also used on osx),bash,ksh, orzsh:sed -i '' $'s/\r$//' winin_unixout.txt
- if have gnu
Comments
Post a Comment