user2158295
user2158295

Reputation:

edit bash command gcc to compile and execute in one line

I want to know if this is possible:

if you put gcc filename.c you will compile filename.c in a.out file and if you put ./a.out you will execute the file.

The composite version it's

gcc filename.c && ./a.out

I want edit this command in bash_profile to do this in one short command line.

gcc filename.c do this gcc filename.c && ./a.out

Upvotes: 0

Views: 1880

Answers (1)

choroba
choroba

Reputation: 241998

You can declare a function:

gccrun () {
    gcc "$1" && ./a.out
}

Be careful, though: what if someone uses full path to the C file as a parameter?

Makefiles are usually used for this kind of stuff.

a.out: file.c
    gcc $<

run: a.out
    ./a.out

make run would run a.out recompiling it if its source has changed since the last compilation or doesn't exist.

Upvotes: 1

Related Questions