2

After updating sed to 4.4 version, sed doesn't replace spaces with commas in the output of find command given to it as here-string:

sed --version
sed (GNU sed) 4.4

ls -l /tmp/test/ total 0 -rw-r--r-- 1 root root 0 Jan 9 17:25 a -rw-r--r-- 1 root root 0 Jan 9 17:25 b

NOT EXPECTED

sed "s: :,:g" <<< $(find /tmp/test/ -type f) /tmp/test/b /tmp/test/a

There are no issue in sed 4.2

sed --version
sed (GNU sed) 4.2.2

ls -l /tmp/test/ total 0 -rw-r--r-- 1 root root 0 Jan 9 17:25 a -rw-r--r-- 1 root root 0 Jan 9 17:25 b

as expected

sed "s: :,:g" <<< $(find /tmp/test/ -type f) /tmp/test/a,/tmp/test/b

As a workaround, storing the result in variable and using echo helps:

a=$(find /tmp/test/ -type f)
echo $a | sed "s: :,:g" 
/tmp/test/b,/tmp/test/a

How to achieve the same output in sed 4.4 using here-string ?

Update

version of bash changed as well between the two systems:

bash --version
GNU bash, version 4.4.20

old version

bash --version
GNU bash, version 4.3.48

1 Answers1

3

This is a change between bash versions 4.3 and 4.4

Bash no longer splits the expansion of here-strings, as the documentation has always said.

The correct behaviour is your new version as you were relying on a bug with your old code.

This will give you a comma-separated list of files,

find -type f | tr '\n' , | sed 's/,$/\n/'

However, seeing as filenames themselves can contain newlines and commas, it's all too easy to break this kind of fragile code. If you care to share your processing - in a new question - I'm sure someone will recommend a better way of processing the file names reliably and safely.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287