How to output a list of files on bash command line

I am using handlebars.js to render client-side templates (using Jade on the node.js server-side) for our single-page app.

I want to precompile the handlebars templates on the server side and bundle them up as one JS file to send to the client.

Currently, I use handlebars to compile templates like so:

$ handlebars template1.handlebars template2.handlebars -f precompiled_templates.js

I'd like to write a bash script that can read all the *.handlebars files in a directory and then run them through the handlebars compiler. So, if I had a directory like so:

templates/
  temp1.handlebars
  temp2.handlebars
  temp3.handlebars
  temp4.handlebars

Running my bash script (or, one line command) in the templates directory would essentially run the following handlebars command:

$ handlebars temp1.handlebars temp2.handlebars temp3.handlebars temp4.handlebars -f precompiled_templates.js

Does anyone know how I could write a bash script to get all the handlebars files in a directory onto the command line like above?

It looks like you want something similar to

handlebars templates/*.handlebars -f precompiled_templates.js

except you would end up with 'templates' prefixed to each file. My preferred approach requires two lines:

files=( templates/*.handlebars )
handlebars "${files[@]#templates/}" -f precompiled_templates.js.

The first line puts all the desired files in an array. In the second line, we expand the contents of the array, but stripping the "templates/" prefix from each element in the resulting expansion.

In the templates directory do:

handlebars `find -name *.handlebars` -f precompiled_templates.js

The back-ticks means it will execute that command then return the result, so you in effect you run handlebars on each file it returns. As written above find will look in current and subdirectories for the files so make sure you run it from the right place.

In Bash, lists can be create with string within double quote :

FILES="/etc/hosts /etc/passwd"
for file in $FILES; do cat $file ; done

<cat all files>

You can also use find and exec commands.

man [find|exec] for more informations