Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

So 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

How ever you would do that in bash. I dunno, because I have almost no bash experience.

share|improve this question
add comment

2 Answers 2

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:

filename="${1%.*}"

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
add comment

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
add comment

Your Answer

 
discard

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.