Custom alias to run and compile .c file

I’m trying to create custom alias to to compile and run my .c files in one command. I tried to add this line in /.bashrc_aliases alias runc='gcc $1 -lm && ./a.out' however I get an error and it seems like the .c file doesn’t get passed to the gcc. Any help apreciated.

I don’t think you can include a variable within an alias. And in any case, you cannot use a variable within single quotes, you’d have to use doublequotes instead.

This function should work. Place these lines in your .bashrc file or wherever you would put the alias.

runc () {
    gcc "$1" -lm && ./a.out

And don’t forget to source your .bashrc file after you have saved your edits.

. ~/.bashrc
Answered By: mchid
Categories: Answers Tags: , , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.