0

I want to read contents of a file using a linux shell script. The contents of file_list.txt is:

abc
def
ghi

And the script to read this is read_file_content.sh:

#!/bin/bash

for file in $(cat file_list.txt)
do
 "processing "$file
done

When I run the command as ./read_file_content.sh, I get the following error:

./read_file_content.sh: line 6: processing abc: command not found
./read_file_content.sh: line 6: processing def: command not found
./read_file_content.sh: line 6: processing ghi: command not found

Why does this print 'command not found'?

Unknown
  • 5,722
  • 5
  • 43
  • 64
Muhammad
  • 611
  • 7
  • 14
  • 32

1 Answers1

4

You wrote "processing "$file without any command in front of it. Bash will take this literally and try to execute it as a command. To print the text on the screen, you can use echo or printf.

Echo example

echo "processing" "$file"

Printf example

printf "%s\n" "$file"

(This is the recommend way if you're going to process weird filenames that contain - and space characters. See Why is printf better than echo?)

Notice the way I did the quotes, this prevents problems with filenames that contain special characters like stars and spaces.

Community
  • 1
  • 1
Ferrybig
  • 18,194
  • 6
  • 57
  • 79