5

I just want to make a 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 I would do that in bash.

10

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
0

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").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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