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
Share:
15,471

Related videos on Youtube

netblognet
Author by

netblognet

Updated on September 18, 2022

Comments

  • netblognet
    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
    Philippos almost 7 years
    That's right, but only part of the problem.
  • Eirik Fuller
    Eirik Fuller almost 7 years
    My testing did not reveal a problem with whitespace.
  • Philippos
    Philippos almost 7 years
    You are right, excuse me.
  • javaamtho
    javaamtho almost 7 years
    +1 this is the better way to store a list of files.
  • netblognet
    netblognet almost 7 years
    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?
  • Angel Todorov
    Angel Todorov almost 7 years
    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