while read p
do
[ -z "$para" ] && para="'${p}'" || para="${para},'${p}'"
done < file
a few days ago, there was issue pop up. in the file, there is empty line at end of the file which causes the list became 'a', 'b', 'c', '' .
I know I can remove the line with sed or other command. but just wondering if it is possible to add condition if p is '' then don't concanated.
You'll notice that the first one evaluates to -n and the second one to -n ''
In the first case the there is no protection of variable p, which contains an empty field. Since there is not protective quotes around $p , the result is field split and since $p is empty this results in 0 fields. So wat remains "-n" which is a test if the string "-n" is non empty
( [ "$somestring" ] if the same as [ -n "$somestring"] , I prefer the latter for clarity, so in fact [ -n ] is equivalent to [ -n -n ] )
In the second case the result of "$p" is not field split and so the -n is interpreted as an operator with one field as parameter, which contains an empty variable, and this evaluates to false .
So that is yet another reason to put quotes around variable expansions.
-n tests the following token for non-empty.
But the [ ] is a test command, and does not see the empty value of $p at all, but it sees a "" as an empty token.
The result for -n without a following token can be an error or the -n itself is seen as a token (and results in a true).
So far the [ ] test command.
For the [[ ]] compound the parser is different:
the $p is seen as a token regardless if its value is empty or not. [[ -n $p ]] should work as you expect.