Use asterisk in variables
15,471
Solution 1
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"
a*
$ echo $files
a1 a2 a3
Solution 2
You're missing a $
in the cat
command which uses input_files
. Try
cat $input_files >> out_file.bak
Solution 3
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
Related videos on Youtube
Author by
netblognet
Updated on September 18, 2022Comments
-
netblognet over 1 year
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:
input_files="file"*".txt" 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?)
-
Philippos almost 7 yearsThat's right, but only part of the problem.
-
Eirik Fuller almost 7 yearsMy testing did not reveal a problem with whitespace.
-
Philippos almost 7 yearsYou are right, excuse me.
-
javaamtho almost 7 years+1 this is the better way to store a list of files.
-
netblognet almost 7 yearsBut 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?
-
Angel Todorov almost 7 yearsNo.
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 withfile with space.
That's because there are no unquoted spaces in the pattern. Create a few sample files and test it for yourself