Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does Shell script create an additional file when only one file is specified

I came across some strange behavior when executing certain Linux commands on different machines. The commands are the ones I provided in the my answer to this question

When I run in Bash and on my Macbook Pro, I get the following results:

$ ls
test.txt    test2.txt   test3.txt   test4.txt

$ (ls *.txt > output) && (sed -i -e 's/^/-v /' output)

$ ls 
output      test.txt    test3.txt
output-e    test2.txt   test4.txt

$ cat output
-v test.txt
-v test2.txt
-v test3.txt
-v test4.txt

$ cat output-e
test.txt
test2.txt
test3.txt
test4.txt

There are two files generated – output and output-e.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now, output-e just has the name of the *.txt files (this is not expected), but the other one has the proper output with the "-v" in front (which is the expected output).

However, when I run this on a Linux system, it simply just gives me the correct output file. Does anyone know what might be causing this extra file output-e to be generated?

>Solution :

You are using BSD version of sed. The option -i takes an argument that specifies the suffix of the backup file. You specified -e as the suffix, so a backup file output-e is created.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading