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 bash_script_name $().asm
nasm -f elf $().asm
ld -s -o $() $().o io.o
... however I would do that in bash.
bash shell-script
add a comment |
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.
bash shell-script
add a comment |
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.
bash shell-script
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.
bash shell-script
bash shell-script
edited 1 hour ago
Rui F Ribeiro
41.5k1483140
41.5k1483140
asked Nov 13 '10 at 21:57
ChigginsChiggins
1604
1604
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
add a 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").
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f4041%2fsimple-variable-bash-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
edited Nov 13 '10 at 23:25
answered Nov 13 '10 at 22:13
Michael Mrozek♦Michael Mrozek
61.8k29192212
61.8k29192212
add a comment |
add a 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").
add a 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").
add a 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").
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").
edited May 23 '17 at 11:33
Community♦
1
1
answered Jun 10 '12 at 15:25
killermistkillermist
954922
954922
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f4041%2fsimple-variable-bash-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown