How do I execute/run c files (in the bash script) after I compile them within a bash script?

For example I use:

for i in `ls *.c`
  gcc $i -o "${i%.c}"
  //execute compiled i.c file here
New contributor
newbieBashdude is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Just execute the resultant executable in the normal way:


for i in *.c
  gcc -Wall -Wextra "$i" -o "${i%.c}"  &&  "./${i%.c}"

N.B. not parsing ls output, quoting arguments, improved compiler warnings, and using && to execute only if compilation succeeded.

  • Thanks for the help, I solved it by making a STR set to pwd, redefined str to append "/${i%.c}" to end of it so that ultimately it would execute each file from having the full path of the file – newbieBashdude Oct 9 at 19:31

Your Answer

newbieBashdude is a new contributor. Be nice, and check out our Code of Conduct.

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.