I have this short if-then-else script:
for INV in "$(ls np4178/*pdf)" ;\
do INVNUMB="$(pdfgrep -ho 'IN[0-9]{6,6}' $INV)" ; \
if [[ -z ${INVNUMB+x} ]]; \
then \
echo "\n$INVNUMB" ; \
else \
echo "wrong \n$INVNUMB" ; \
fi ; \
done
Which produces this:
wrong \nIN353886
IN353897
IN353905
IN353910
IN353902
IN353864
IN353875
IN353840
IN353862
IN353922
IN353739
IN353876
IN353920
However, if I make a change to the else
statement I get this:
for INV in "$(ls np4178/*pdf)" ;\
do INVNUMB="$(pdfgrep -ho 'IN[0-9]{6,6}' $INV)" ; \
if [[ -z ${INVNUMB+x} ]]; \
then \
echo "\n$INVNUMB" ; \
else \
echo "\nIN000000" ; \
fi ; \
done
Then I only get this:
\nIN000000
Why? How can changing the text string in the else
clause the entire behaviour and results of the script to change?
Why is the newline character \n
printed as a literal in the else
clause?
xpg_echo
shopt
option was set,\n
would be special toecho
; but, then, everyecho "wrong \n$INVNUMB"
would printwrong
+ a newline + the expansion of$INVNUMB
. This is not what we see, telling us thatxpg_echo
is not set. Hence, (sinceecho -e
is not used)\n
is not special toecho
, meaning that your first output snippet is produced by a singleecho "wrong \n$INVNUMB"
. Which seems consistent with the result you get when you change"wrong \n$INVNUMB"
into"\nIN000000"
. – fra-san May 11 '21 at 20:22