how to assign a function with arguments to a variable, then pass the variable to another function in shell...
I'm new to shell script, so any information is not trivial at all.
I'm writing a generic function create_dir that take a variable $dirname and an option variable $function to generate a directory. If the optional $function is absent, the default function is mkdir. Like this:
File ~/bin/lib/create_dir.sh:
#!/bin/bash
create_dir()
{
DIRNAME=$1
FUNCTION=${2:-mkdir}
$FUNCTION $DIRNAME
}
export -f create_dir
That works fine.
Now I import this into another file ~/bin/create_app
#!/bin/bash
. "${HOME}/bin/lib/create_dir.sh"
DIRNAME=$1
FUNCTION="python manage.py startapp"
create_dir $FUNCTION $DIRNAME
When I run create_app it imported the create_dir but the variable $FUNCTION it feeds the create_dir is wrong. The variable $FUNCTION has only one word python instead of python manage.py startapp as I wanted.
Why? How to fix it?
command-line bash scripts
add a comment |
I'm new to shell script, so any information is not trivial at all.
I'm writing a generic function create_dir that take a variable $dirname and an option variable $function to generate a directory. If the optional $function is absent, the default function is mkdir. Like this:
File ~/bin/lib/create_dir.sh:
#!/bin/bash
create_dir()
{
DIRNAME=$1
FUNCTION=${2:-mkdir}
$FUNCTION $DIRNAME
}
export -f create_dir
That works fine.
Now I import this into another file ~/bin/create_app
#!/bin/bash
. "${HOME}/bin/lib/create_dir.sh"
DIRNAME=$1
FUNCTION="python manage.py startapp"
create_dir $FUNCTION $DIRNAME
When I run create_app it imported the create_dir but the variable $FUNCTION it feeds the create_dir is wrong. The variable $FUNCTION has only one word python instead of python manage.py startapp as I wanted.
Why? How to fix it?
command-line bash scripts
1
You should quote the argument like this:"$FUNCTION". Otherwise, the shell will runcreate_dir python manage.py startapp ...followed by all the words in the first argument tocreate_app. This way the first argument will bepythonand the second onemanage.py
– Stefan Hamcke
Feb 1 at 19:33
Stefan is right. Important rule in shell programming: When in doubt: quote. To check your scripts you can use shellcheck
– Ralf
Feb 1 at 19:39
oh my god I saw somewhere they emphasized the importance of putting variables between double quotes. Now I seee
– Linh Chi Nguyen
Feb 1 at 19:39
add a comment |
I'm new to shell script, so any information is not trivial at all.
I'm writing a generic function create_dir that take a variable $dirname and an option variable $function to generate a directory. If the optional $function is absent, the default function is mkdir. Like this:
File ~/bin/lib/create_dir.sh:
#!/bin/bash
create_dir()
{
DIRNAME=$1
FUNCTION=${2:-mkdir}
$FUNCTION $DIRNAME
}
export -f create_dir
That works fine.
Now I import this into another file ~/bin/create_app
#!/bin/bash
. "${HOME}/bin/lib/create_dir.sh"
DIRNAME=$1
FUNCTION="python manage.py startapp"
create_dir $FUNCTION $DIRNAME
When I run create_app it imported the create_dir but the variable $FUNCTION it feeds the create_dir is wrong. The variable $FUNCTION has only one word python instead of python manage.py startapp as I wanted.
Why? How to fix it?
command-line bash scripts
I'm new to shell script, so any information is not trivial at all.
I'm writing a generic function create_dir that take a variable $dirname and an option variable $function to generate a directory. If the optional $function is absent, the default function is mkdir. Like this:
File ~/bin/lib/create_dir.sh:
#!/bin/bash
create_dir()
{
DIRNAME=$1
FUNCTION=${2:-mkdir}
$FUNCTION $DIRNAME
}
export -f create_dir
That works fine.
Now I import this into another file ~/bin/create_app
#!/bin/bash
. "${HOME}/bin/lib/create_dir.sh"
DIRNAME=$1
FUNCTION="python manage.py startapp"
create_dir $FUNCTION $DIRNAME
When I run create_app it imported the create_dir but the variable $FUNCTION it feeds the create_dir is wrong. The variable $FUNCTION has only one word python instead of python manage.py startapp as I wanted.
Why? How to fix it?
command-line bash scripts
command-line bash scripts
asked Feb 1 at 19:30
Linh Chi NguyenLinh Chi Nguyen
63
63
1
You should quote the argument like this:"$FUNCTION". Otherwise, the shell will runcreate_dir python manage.py startapp ...followed by all the words in the first argument tocreate_app. This way the first argument will bepythonand the second onemanage.py
– Stefan Hamcke
Feb 1 at 19:33
Stefan is right. Important rule in shell programming: When in doubt: quote. To check your scripts you can use shellcheck
– Ralf
Feb 1 at 19:39
oh my god I saw somewhere they emphasized the importance of putting variables between double quotes. Now I seee
– Linh Chi Nguyen
Feb 1 at 19:39
add a comment |
1
You should quote the argument like this:"$FUNCTION". Otherwise, the shell will runcreate_dir python manage.py startapp ...followed by all the words in the first argument tocreate_app. This way the first argument will bepythonand the second onemanage.py
– Stefan Hamcke
Feb 1 at 19:33
Stefan is right. Important rule in shell programming: When in doubt: quote. To check your scripts you can use shellcheck
– Ralf
Feb 1 at 19:39
oh my god I saw somewhere they emphasized the importance of putting variables between double quotes. Now I seee
– Linh Chi Nguyen
Feb 1 at 19:39
1
1
You should quote the argument like this:
"$FUNCTION". Otherwise, the shell will run create_dir python manage.py startapp ... followed by all the words in the first argument to create_app. This way the first argument will be python and the second one manage.py– Stefan Hamcke
Feb 1 at 19:33
You should quote the argument like this:
"$FUNCTION". Otherwise, the shell will run create_dir python manage.py startapp ... followed by all the words in the first argument to create_app. This way the first argument will be python and the second one manage.py– Stefan Hamcke
Feb 1 at 19:33
Stefan is right. Important rule in shell programming: When in doubt: quote. To check your scripts you can use shellcheck
– Ralf
Feb 1 at 19:39
Stefan is right. Important rule in shell programming: When in doubt: quote. To check your scripts you can use shellcheck
– Ralf
Feb 1 at 19:39
oh my god I saw somewhere they emphasized the importance of putting variables between double quotes. Now I seee
– Linh Chi Nguyen
Feb 1 at 19:39
oh my god I saw somewhere they emphasized the importance of putting variables between double quotes. Now I seee
– Linh Chi Nguyen
Feb 1 at 19:39
add a comment |
1 Answer
1
active
oldest
votes
You should quote the argument like this: "$FUNCTION". Otherwise, the shell will run
create_dir python manage.py startapp ...
followed by all the words in the first argument to create_app. This way the first argument to create_dir will be python and the second one manage.py.
You are always on the safe side if you put double quotes around your variable expansions. This will prevent the shell from splitting the value of that variable into separate words, which the function then regards as different arguments when you pass to it that variable as an argument.
There are cases where you can go without quotes, for example in the line
DIRNAME=$1
or more generally
var2=$var
as the bash will automatically assume that you want to assign the entire value of var to the variable var2. On the other hand, if you write the value directly, you need to use quotes:
string="Hello World"
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2faskubuntu.com%2fquestions%2f1114817%2fhow-to-assign-a-function-with-arguments-to-a-variable-then-pass-the-variable-to%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should quote the argument like this: "$FUNCTION". Otherwise, the shell will run
create_dir python manage.py startapp ...
followed by all the words in the first argument to create_app. This way the first argument to create_dir will be python and the second one manage.py.
You are always on the safe side if you put double quotes around your variable expansions. This will prevent the shell from splitting the value of that variable into separate words, which the function then regards as different arguments when you pass to it that variable as an argument.
There are cases where you can go without quotes, for example in the line
DIRNAME=$1
or more generally
var2=$var
as the bash will automatically assume that you want to assign the entire value of var to the variable var2. On the other hand, if you write the value directly, you need to use quotes:
string="Hello World"
add a comment |
You should quote the argument like this: "$FUNCTION". Otherwise, the shell will run
create_dir python manage.py startapp ...
followed by all the words in the first argument to create_app. This way the first argument to create_dir will be python and the second one manage.py.
You are always on the safe side if you put double quotes around your variable expansions. This will prevent the shell from splitting the value of that variable into separate words, which the function then regards as different arguments when you pass to it that variable as an argument.
There are cases where you can go without quotes, for example in the line
DIRNAME=$1
or more generally
var2=$var
as the bash will automatically assume that you want to assign the entire value of var to the variable var2. On the other hand, if you write the value directly, you need to use quotes:
string="Hello World"
add a comment |
You should quote the argument like this: "$FUNCTION". Otherwise, the shell will run
create_dir python manage.py startapp ...
followed by all the words in the first argument to create_app. This way the first argument to create_dir will be python and the second one manage.py.
You are always on the safe side if you put double quotes around your variable expansions. This will prevent the shell from splitting the value of that variable into separate words, which the function then regards as different arguments when you pass to it that variable as an argument.
There are cases where you can go without quotes, for example in the line
DIRNAME=$1
or more generally
var2=$var
as the bash will automatically assume that you want to assign the entire value of var to the variable var2. On the other hand, if you write the value directly, you need to use quotes:
string="Hello World"
You should quote the argument like this: "$FUNCTION". Otherwise, the shell will run
create_dir python manage.py startapp ...
followed by all the words in the first argument to create_app. This way the first argument to create_dir will be python and the second one manage.py.
You are always on the safe side if you put double quotes around your variable expansions. This will prevent the shell from splitting the value of that variable into separate words, which the function then regards as different arguments when you pass to it that variable as an argument.
There are cases where you can go without quotes, for example in the line
DIRNAME=$1
or more generally
var2=$var
as the bash will automatically assume that you want to assign the entire value of var to the variable var2. On the other hand, if you write the value directly, you need to use quotes:
string="Hello World"
edited Feb 1 at 20:03
answered Feb 1 at 19:49
Stefan HamckeStefan Hamcke
4851622
4851622
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1114817%2fhow-to-assign-a-function-with-arguments-to-a-variable-then-pass-the-variable-to%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
1
You should quote the argument like this:
"$FUNCTION". Otherwise, the shell will runcreate_dir python manage.py startapp ...followed by all the words in the first argument tocreate_app. This way the first argument will bepythonand the second onemanage.py– Stefan Hamcke
Feb 1 at 19:33
Stefan is right. Important rule in shell programming: When in doubt: quote. To check your scripts you can use shellcheck
– Ralf
Feb 1 at 19:39
oh my god I saw somewhere they emphasized the importance of putting variables between double quotes. Now I seee
– Linh Chi Nguyen
Feb 1 at 19:39