Heptic
Heptic

Reputation: 3106

Customized bash wildcard expansion

If I do

*.c

it will return a list of files ending with .c, separated by a space. However, I'd like it to instead return a list of files, prefixed with #include " and postfixed with "\n.

e.g. output

#include "blah.c"
#include "meh.c"
#include "hehe.c"

I want to pipe the results of this into gcc -xc - so bonus points if the command is a one-liner.

(And no, I don't want to just cat *.c because it'll lose source-location information)

Upvotes: 0

Views: 378

Answers (3)

Gordon Davisson
Gordon Davisson

Reputation: 125848

There's a simpler way:

printf '#include "%s"\n' *.c

Upvotes: 2

johnnycrash
johnnycrash

Reputation: 5344

I didnt test this, but this might do it

for a in *.c ; do echo '#include "'$a'"' ; done | gcc -xc -

Upvotes: 1

Perhaps something like

for f in *.c ; do
  printf '#include "%s"\n' $f
done | gcc -xc -

should do what you want

However, gcc accepts multiple source files, so you could just do

gcc -Wall *.c -o yourprog -lyourlibrary

You could also pass -g -O -flto to gcc

Upvotes: 2

Related Questions