Simple "Variable" Bash File
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
nasm -f elf $().asm ld -s -o $() $().o io.o
… however I would do that in bash.
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
Then you can use
$filename in your script wherever you want:
nasm -f elf "$filename.asm" ld -s -o "$filename" "$filename".o io.o
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”).