I want to cat all files to one new file in a bash script.

For example there are three files in my dir: - file_a.txt - file b.txt - file(c).txt

When I write the following, it works without problems:

cat "file"*".txt" >> out_file.bak

No I want to make it more flexible/clean by using a variable:

cat $input_files >> out_file.bak

Unfortunately this doesn't work. The question is why? (When I echo input_files and run the command in terminal everything is fine. So why doesn't it work in the bash script?)


It depends when you want to do the expansion:

When you define the variable:

$ files=$(echo a*)
$ echo $files
a1 a2 a3
$ echo "$files"
a1 a2 a3

When you access the variable:

$ files=a*
$ echo "$files"
$ echo $files
a1 a2 a3

You're missing a $ in the cat command which uses input_files. Try

cat $input_files >> out_file.bak
  • That's right, but only part of the problem. – Philippos Jul 13 '17 at 12:20
  • My testing did not reveal a problem with whitespace. – Eirik Fuller Jul 13 '17 at 12:25
  • You are right, excuse me. – Philippos Jul 13 '17 at 12:29

You'll have to take more care if you have filenames containing whitespace. In that case, use an array:

input_files=( "file with space."*.txt )
cat "${input_files[@]}" >> out_file.bak
  • +1 this is the better way to store a list of files. – Gordon Davisson Jul 13 '17 at 19:42
  • But this would also output files named "file with space." (Which is missing the *.txt part). Or does cat know that each part of the array must match? – netblognet Jul 14 '17 at 11:05
  • No. cat doesn't "know" anything: the shell expands the glob pattern into the array, so the array contains just a list of filenames. Then the array expansion syntax provides cat with all the filenames. The glob pattern "file with space."*.txt will only match files ending with .txt and beginning with file with space. That's because there are no unquoted spaces in the pattern. Create a few sample files and test it for yourself – glenn jackman Jul 14 '17 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.