Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I just want to make a simple bash file that takes a file name as a parameter. In that bash file, I want to take the name before the extension and have it as a "variable", to use in other places. For example, if I was to run bash_script_name sample.asm, the bash script would run:

nasm -f elf sample.asm
ld -s -o sample sample.o io.o

So basically the form of bash_script_name $().asm

nasm -f elf $().asm
ld -s -o $() $().o io.o

... however you would do that in bash. I don't know, because I have almost no bash experience.

share|improve this question
up vote 10 down vote accepted

The first argument to the script will be in $1. You can use a bash string replacement to pull the extension; this removes everything from the last occurrence of . forward, and stores the result in $filename:


Then you can use $filename in your script wherever you want:

nasm -f elf "$filename.asm"
ld -s -o "$filename" "$filename".o io.o
share|improve this answer

This particular answer to a similar question on Stack Overflow seems to have been very well thought out. It includes logic to deal with files with no dots in them ("somefile"), and files with multiple dots in them ("somefile.tar.gz").

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.