Marc Adler
Marc Adler

Reputation: 534

Manipulating command-line arguments in bash

I want to create a shell script so that I can type

gc program_name.c

and achieve the same effect as if I had typed

gcc -o program_name program_name.c

Now, I know how to do this so that I can simply type gcc program_name and get the effect:

gcc -o $1 $1.c

The problem is I want to use tab completion and this method requires me to backspace to delete the extension. (Yes, it's a picayune thing, but I'm interested in learning the general principle behind this kind of argument manipulation, too.)

In other words, I want to be able to have the script delete the trailing extension. I'm guessing I can use another variable, but I'm not sure how to say, for example, $name = $1 minus the trailing extension.

Thanks.

Upvotes: 1

Views: 509

Answers (3)

Desislav Kamenov
Desislav Kamenov

Reputation: 1203

For unix use

name=`basename $1 ".c"`

Upvotes: 1

Charles Duffy
Charles Duffy

Reputation: 295520

#!/bin/bash
exec gcc -o "${1%.c}" "$1" 

Upvotes: 2

Geordee Naliyath
Geordee Naliyath

Reputation: 1859

See whether you can take it forward from the following snippet.

pgm=test.c
echo ${pgm%%.*}

Upvotes: 0

Related Questions