passing export command to function in shell script
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
#!/bin/sh
execute_cmd()
$($@)
execute_cmd export MY_VAR=my_val
echo $MY_VAR
Since $()
executes in a sub-shell, $MY_VAR
isn't set properly in the shell the script is running.
My question, how can I pass the export command to a function and execute it in the current shell the script is running in?
shell function
add a comment |Â
up vote
1
down vote
favorite
#!/bin/sh
execute_cmd()
$($@)
execute_cmd export MY_VAR=my_val
echo $MY_VAR
Since $()
executes in a sub-shell, $MY_VAR
isn't set properly in the shell the script is running.
My question, how can I pass the export command to a function and execute it in the current shell the script is running in?
shell function
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
#!/bin/sh
execute_cmd()
$($@)
execute_cmd export MY_VAR=my_val
echo $MY_VAR
Since $()
executes in a sub-shell, $MY_VAR
isn't set properly in the shell the script is running.
My question, how can I pass the export command to a function and execute it in the current shell the script is running in?
shell function
#!/bin/sh
execute_cmd()
$($@)
execute_cmd export MY_VAR=my_val
echo $MY_VAR
Since $()
executes in a sub-shell, $MY_VAR
isn't set properly in the shell the script is running.
My question, how can I pass the export command to a function and execute it in the current shell the script is running in?
shell function
shell function
edited Aug 28 '17 at 23:32
Gilles
513k12010191549
513k12010191549
asked Aug 28 '17 at 16:51
user3763392
82
82
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Use eval
instead of $()
:
execute_cmd()
eval $@
From bash manual:
eval [arguments]
The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of eval. If there are no arguments or only empty arguments, the return status is zero.
Other shells usually have eval
built-in with similar semantics.
Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g.eval $@ 2>&1 | tee -a $logfile
â user3763392
Aug 28 '17 at 17:52
Usual output redirection:eval echo "Hello World!" > log.txt
, orexecute_cmd echo "Hello World" > log.txt
. Note the latter redirects the output ofexecute_cmd
instead of passing those characters to the function. To redirect inexecute_cmd
pass the redirection as string:execute_cmd 'echo "Hello World" > log.txt'
.
â sebasth
Aug 28 '17 at 17:59
1
eval $@
doesn't make sense. It should probably be either"$@"
oreval "$1"
, or possiblyeval "$@"
(which makesexecute_cmd
redundant since it's equivalent toeval
).
â Gilles
Aug 28 '17 at 22:40
add a comment |Â
up vote
2
down vote
It isn't clear what you want to do. $($@)
doesn't make sense, but what do you want to do instead?
What $($@)
does:
- Take the arguments of the function. This is a list of strings.
- Split each piece further where they contain whitespaceù.
- Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.
- Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.
- Take the output of the command and split it where it contains whitespace.
- Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.
- Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.
If that sounds complicated, that's because it is.
If you want execute_cmd export MY_VAR=my_val
to execute export MY_VAR=my_val
, why are you even bothering with execute_cmd
?
There are two ways this could be interpreted sensibly.
You want to pass a command with parameters to a function. A command with parameters is a list of strings, the first being a function name, shell builtin or executable file. Then the syntax to invoke this command on the supplied parameters is
"$@"
.execute_cmd ()
"$@"
execute_cmd export MY_VAR=my_valThe double quotes avoid the splitting and wildcard expansion steps I mentioned above. Always use double quotes around variable substitutions.
You want to run a shell snippet. A shell snippet is a single string, to be passed as a single argument. To run a string containing shell code, use the
eval
builtin.execute_cmd ()
eval "$1"
execute_cmd 'export MY_VAR=my_val'
ù Assuming the default IFS
. If you know about that you don't need to read this paragraph.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Use eval
instead of $()
:
execute_cmd()
eval $@
From bash manual:
eval [arguments]
The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of eval. If there are no arguments or only empty arguments, the return status is zero.
Other shells usually have eval
built-in with similar semantics.
Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g.eval $@ 2>&1 | tee -a $logfile
â user3763392
Aug 28 '17 at 17:52
Usual output redirection:eval echo "Hello World!" > log.txt
, orexecute_cmd echo "Hello World" > log.txt
. Note the latter redirects the output ofexecute_cmd
instead of passing those characters to the function. To redirect inexecute_cmd
pass the redirection as string:execute_cmd 'echo "Hello World" > log.txt'
.
â sebasth
Aug 28 '17 at 17:59
1
eval $@
doesn't make sense. It should probably be either"$@"
oreval "$1"
, or possiblyeval "$@"
(which makesexecute_cmd
redundant since it's equivalent toeval
).
â Gilles
Aug 28 '17 at 22:40
add a comment |Â
up vote
0
down vote
accepted
Use eval
instead of $()
:
execute_cmd()
eval $@
From bash manual:
eval [arguments]
The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of eval. If there are no arguments or only empty arguments, the return status is zero.
Other shells usually have eval
built-in with similar semantics.
Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g.eval $@ 2>&1 | tee -a $logfile
â user3763392
Aug 28 '17 at 17:52
Usual output redirection:eval echo "Hello World!" > log.txt
, orexecute_cmd echo "Hello World" > log.txt
. Note the latter redirects the output ofexecute_cmd
instead of passing those characters to the function. To redirect inexecute_cmd
pass the redirection as string:execute_cmd 'echo "Hello World" > log.txt'
.
â sebasth
Aug 28 '17 at 17:59
1
eval $@
doesn't make sense. It should probably be either"$@"
oreval "$1"
, or possiblyeval "$@"
(which makesexecute_cmd
redundant since it's equivalent toeval
).
â Gilles
Aug 28 '17 at 22:40
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Use eval
instead of $()
:
execute_cmd()
eval $@
From bash manual:
eval [arguments]
The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of eval. If there are no arguments or only empty arguments, the return status is zero.
Other shells usually have eval
built-in with similar semantics.
Use eval
instead of $()
:
execute_cmd()
eval $@
From bash manual:
eval [arguments]
The arguments are concatenated together into a single command, which is then read and executed, and its exit status returned as the exit status of eval. If there are no arguments or only empty arguments, the return status is zero.
Other shells usually have eval
built-in with similar semantics.
edited 10 mins ago
answered Aug 28 '17 at 16:58
sebasth
7,44631745
7,44631745
Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g.eval $@ 2>&1 | tee -a $logfile
â user3763392
Aug 28 '17 at 17:52
Usual output redirection:eval echo "Hello World!" > log.txt
, orexecute_cmd echo "Hello World" > log.txt
. Note the latter redirects the output ofexecute_cmd
instead of passing those characters to the function. To redirect inexecute_cmd
pass the redirection as string:execute_cmd 'echo "Hello World" > log.txt'
.
â sebasth
Aug 28 '17 at 17:59
1
eval $@
doesn't make sense. It should probably be either"$@"
oreval "$1"
, or possiblyeval "$@"
(which makesexecute_cmd
redundant since it's equivalent toeval
).
â Gilles
Aug 28 '17 at 22:40
add a comment |Â
Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g.eval $@ 2>&1 | tee -a $logfile
â user3763392
Aug 28 '17 at 17:52
Usual output redirection:eval echo "Hello World!" > log.txt
, orexecute_cmd echo "Hello World" > log.txt
. Note the latter redirects the output ofexecute_cmd
instead of passing those characters to the function. To redirect inexecute_cmd
pass the redirection as string:execute_cmd 'echo "Hello World" > log.txt'
.
â sebasth
Aug 28 '17 at 17:59
1
eval $@
doesn't make sense. It should probably be either"$@"
oreval "$1"
, or possiblyeval "$@"
(which makesexecute_cmd
redundant since it's equivalent toeval
).
â Gilles
Aug 28 '17 at 22:40
Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g.
eval $@ 2>&1 | tee -a $logfile
â user3763392
Aug 28 '17 at 17:52
Follow up question. What if I'm needing to redirect the stdout and stderr of the argument passed to the function, to a logfile. e.g.
eval $@ 2>&1 | tee -a $logfile
â user3763392
Aug 28 '17 at 17:52
Usual output redirection:
eval echo "Hello World!" > log.txt
, or execute_cmd echo "Hello World" > log.txt
. Note the latter redirects the output of execute_cmd
instead of passing those characters to the function. To redirect in execute_cmd
pass the redirection as string: execute_cmd 'echo "Hello World" > log.txt'
.â sebasth
Aug 28 '17 at 17:59
Usual output redirection:
eval echo "Hello World!" > log.txt
, or execute_cmd echo "Hello World" > log.txt
. Note the latter redirects the output of execute_cmd
instead of passing those characters to the function. To redirect in execute_cmd
pass the redirection as string: execute_cmd 'echo "Hello World" > log.txt'
.â sebasth
Aug 28 '17 at 17:59
1
1
eval $@
doesn't make sense. It should probably be either "$@"
or eval "$1"
, or possibly eval "$@"
(which makes execute_cmd
redundant since it's equivalent to eval
).â Gilles
Aug 28 '17 at 22:40
eval $@
doesn't make sense. It should probably be either "$@"
or eval "$1"
, or possibly eval "$@"
(which makes execute_cmd
redundant since it's equivalent to eval
).â Gilles
Aug 28 '17 at 22:40
add a comment |Â
up vote
2
down vote
It isn't clear what you want to do. $($@)
doesn't make sense, but what do you want to do instead?
What $($@)
does:
- Take the arguments of the function. This is a list of strings.
- Split each piece further where they contain whitespaceù.
- Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.
- Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.
- Take the output of the command and split it where it contains whitespace.
- Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.
- Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.
If that sounds complicated, that's because it is.
If you want execute_cmd export MY_VAR=my_val
to execute export MY_VAR=my_val
, why are you even bothering with execute_cmd
?
There are two ways this could be interpreted sensibly.
You want to pass a command with parameters to a function. A command with parameters is a list of strings, the first being a function name, shell builtin or executable file. Then the syntax to invoke this command on the supplied parameters is
"$@"
.execute_cmd ()
"$@"
execute_cmd export MY_VAR=my_valThe double quotes avoid the splitting and wildcard expansion steps I mentioned above. Always use double quotes around variable substitutions.
You want to run a shell snippet. A shell snippet is a single string, to be passed as a single argument. To run a string containing shell code, use the
eval
builtin.execute_cmd ()
eval "$1"
execute_cmd 'export MY_VAR=my_val'
ù Assuming the default IFS
. If you know about that you don't need to read this paragraph.
add a comment |Â
up vote
2
down vote
It isn't clear what you want to do. $($@)
doesn't make sense, but what do you want to do instead?
What $($@)
does:
- Take the arguments of the function. This is a list of strings.
- Split each piece further where they contain whitespaceù.
- Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.
- Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.
- Take the output of the command and split it where it contains whitespace.
- Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.
- Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.
If that sounds complicated, that's because it is.
If you want execute_cmd export MY_VAR=my_val
to execute export MY_VAR=my_val
, why are you even bothering with execute_cmd
?
There are two ways this could be interpreted sensibly.
You want to pass a command with parameters to a function. A command with parameters is a list of strings, the first being a function name, shell builtin or executable file. Then the syntax to invoke this command on the supplied parameters is
"$@"
.execute_cmd ()
"$@"
execute_cmd export MY_VAR=my_valThe double quotes avoid the splitting and wildcard expansion steps I mentioned above. Always use double quotes around variable substitutions.
You want to run a shell snippet. A shell snippet is a single string, to be passed as a single argument. To run a string containing shell code, use the
eval
builtin.execute_cmd ()
eval "$1"
execute_cmd 'export MY_VAR=my_val'
ù Assuming the default IFS
. If you know about that you don't need to read this paragraph.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
It isn't clear what you want to do. $($@)
doesn't make sense, but what do you want to do instead?
What $($@)
does:
- Take the arguments of the function. This is a list of strings.
- Split each piece further where they contain whitespaceù.
- Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.
- Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.
- Take the output of the command and split it where it contains whitespace.
- Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.
- Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.
If that sounds complicated, that's because it is.
If you want execute_cmd export MY_VAR=my_val
to execute export MY_VAR=my_val
, why are you even bothering with execute_cmd
?
There are two ways this could be interpreted sensibly.
You want to pass a command with parameters to a function. A command with parameters is a list of strings, the first being a function name, shell builtin or executable file. Then the syntax to invoke this command on the supplied parameters is
"$@"
.execute_cmd ()
"$@"
execute_cmd export MY_VAR=my_valThe double quotes avoid the splitting and wildcard expansion steps I mentioned above. Always use double quotes around variable substitutions.
You want to run a shell snippet. A shell snippet is a single string, to be passed as a single argument. To run a string containing shell code, use the
eval
builtin.execute_cmd ()
eval "$1"
execute_cmd 'export MY_VAR=my_val'
ù Assuming the default IFS
. If you know about that you don't need to read this paragraph.
It isn't clear what you want to do. $($@)
doesn't make sense, but what do you want to do instead?
What $($@)
does:
- Take the arguments of the function. This is a list of strings.
- Split each piece further where they contain whitespaceù.
- Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.
- Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.
- Take the output of the command and split it where it contains whitespace.
- Take each split piece and interpret it as a wildcard pattern. If the pattern matches any file, replace the piece by the list of matching file names.
- Use the first piece as the name of a command to execute (function, shell builtin or executable file) and pass the other pieces as arguments.
If that sounds complicated, that's because it is.
If you want execute_cmd export MY_VAR=my_val
to execute export MY_VAR=my_val
, why are you even bothering with execute_cmd
?
There are two ways this could be interpreted sensibly.
You want to pass a command with parameters to a function. A command with parameters is a list of strings, the first being a function name, shell builtin or executable file. Then the syntax to invoke this command on the supplied parameters is
"$@"
.execute_cmd ()
"$@"
execute_cmd export MY_VAR=my_valThe double quotes avoid the splitting and wildcard expansion steps I mentioned above. Always use double quotes around variable substitutions.
You want to run a shell snippet. A shell snippet is a single string, to be passed as a single argument. To run a string containing shell code, use the
eval
builtin.execute_cmd ()
eval "$1"
execute_cmd 'export MY_VAR=my_val'
ù Assuming the default IFS
. If you know about that you don't need to read this paragraph.
answered Aug 28 '17 at 23:32
Gilles
513k12010191549
513k12010191549
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f388886%2fpassing-export-command-to-function-in-shell-script%23new-answer', 'question_page');
);
Post as a guest
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
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
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