doxyl
doxyl

Reputation: 49

Specifying gcc output file name in a bash alias

I have begun learning C using ncurses and would like to make the compile command a bit less long winded to type out. The following is the full command I'm using.

gcc example.c -o example -lncurses

What I would like to do is make a bash alias that fulfills the above but only having to type, say...

gcc_alias example.c

I don't have much experience with bash at all but is it possible to make a little one-line alias in my .bashrc to accomplish this?

I've tried on my own and the best I can come up with is:

gcc_alias="gcc $1 -o ____ -lncurses"

Where obviously ____ is the part that I can't figure out.

I've looked for answers but haven't been able to find a similar enough problem. Cheers.

Upvotes: 2

Views: 1768

Answers (1)

Wintermute
Wintermute

Reputation: 44063

This cannot be done with an alias (aliases are strictly text replacement). You can use a shell function like this:

gcc_alias() { gcc -o "${1%.*}" "$1" -lncurses; }

to be called as

gcc_alias example.c

Here ${1%.*} is a parameter expansion that removes the shortest suffix matching .* from $1.

Seriously though, to make compilation manageable, you'll want to take a look at makefiles.

Upvotes: 4

Related Questions