TL;DR
I was writing a very simple bash script, which goes into each folder in my pwd and find for files with names pid
. If yes, echo that file out.
pids=$(cat */*/bin/*pid)
echo $pids
>> 3742031 3741375 3741415 3742159
The PIDs are nicely printed with spaces
I changed this code a bit to find PIDs in path:
pids=$(cat */*pid)
echo $pids
>> 37410543741078
Notice this time it was printed without spaces.
Now all these files are auto generated by an application, so the way of storing these values must be consistent - with or without \n
etc.
My question is with almost the same code, why in one case it concatenates the string with space and in the other case without space, how is it possible to get a more consistent behavior with cat? (not echo).
Adding the workaround:
Regardless of the file, I am appending a whitespace, before reading the file like: sed '$s/$/ /' */pid. This resolves the problem.
echo "$pids"
, what do you get and does it shed any light on the issue?cat
but what's in your files.*/*/bin/*pid
and*/*pid
simultaneously. The simplest answer is you're reading different files and their general format in the first case is different than in the second case. The claim that "the way of storing these values must be consistent" is questionable. Bugs happen, quirks happen, inconsistent requirements happen.