keep environment when executing in script
I have a bash script that should be executing a number of commands. The commands work fine when entered in a termainal but when I try to execute them from inside the script they don't work, so I assume it's something environment-specific.
In the script I have
exec "$cmd"
and it doesn't work, while if I copy the contents of $cmd and paste in a terminal it works fine.
How do I keep the current environment when running the script? Or do you have any other idea what it might be?
bash bash-scripting
add a comment |
I have a bash script that should be executing a number of commands. The commands work fine when entered in a termainal but when I try to execute them from inside the script they don't work, so I assume it's something environment-specific.
In the script I have
exec "$cmd"
and it doesn't work, while if I copy the contents of $cmd and paste in a terminal it works fine.
How do I keep the current environment when running the script? Or do you have any other idea what it might be?
bash bash-scripting
1
Running the script does not change the environment by default. You've misdiagnosed the problem.
– Ignacio Vazquez-Abrams
Aug 22 '12 at 11:29
1
Be sure toexport
the variables needed by the script before running it.
– choroba
Aug 22 '12 at 11:31
Ingacio: Hmkay, then I don't know. I do exec "$command" in the script and it doesn't work, the exact same command in the terminal work. choroba: I'm not sure which variables it is :/ it's a big enterprise environment with scripts and variables all over the place.
– dutt
Aug 22 '12 at 11:36
Is there some special reason you are using exec? Why not just write the command directly? e.g.: `cmd=ls; $cmd;'
– terdon
Aug 22 '12 at 12:56
I tried both and neither worked, is there any difference?
– dutt
Aug 22 '12 at 13:41
add a comment |
I have a bash script that should be executing a number of commands. The commands work fine when entered in a termainal but when I try to execute them from inside the script they don't work, so I assume it's something environment-specific.
In the script I have
exec "$cmd"
and it doesn't work, while if I copy the contents of $cmd and paste in a terminal it works fine.
How do I keep the current environment when running the script? Or do you have any other idea what it might be?
bash bash-scripting
I have a bash script that should be executing a number of commands. The commands work fine when entered in a termainal but when I try to execute them from inside the script they don't work, so I assume it's something environment-specific.
In the script I have
exec "$cmd"
and it doesn't work, while if I copy the contents of $cmd and paste in a terminal it works fine.
How do I keep the current environment when running the script? Or do you have any other idea what it might be?
bash bash-scripting
bash bash-scripting
edited Aug 22 '12 at 11:37
dutt
asked Aug 22 '12 at 11:25
duttdutt
10614
10614
1
Running the script does not change the environment by default. You've misdiagnosed the problem.
– Ignacio Vazquez-Abrams
Aug 22 '12 at 11:29
1
Be sure toexport
the variables needed by the script before running it.
– choroba
Aug 22 '12 at 11:31
Ingacio: Hmkay, then I don't know. I do exec "$command" in the script and it doesn't work, the exact same command in the terminal work. choroba: I'm not sure which variables it is :/ it's a big enterprise environment with scripts and variables all over the place.
– dutt
Aug 22 '12 at 11:36
Is there some special reason you are using exec? Why not just write the command directly? e.g.: `cmd=ls; $cmd;'
– terdon
Aug 22 '12 at 12:56
I tried both and neither worked, is there any difference?
– dutt
Aug 22 '12 at 13:41
add a comment |
1
Running the script does not change the environment by default. You've misdiagnosed the problem.
– Ignacio Vazquez-Abrams
Aug 22 '12 at 11:29
1
Be sure toexport
the variables needed by the script before running it.
– choroba
Aug 22 '12 at 11:31
Ingacio: Hmkay, then I don't know. I do exec "$command" in the script and it doesn't work, the exact same command in the terminal work. choroba: I'm not sure which variables it is :/ it's a big enterprise environment with scripts and variables all over the place.
– dutt
Aug 22 '12 at 11:36
Is there some special reason you are using exec? Why not just write the command directly? e.g.: `cmd=ls; $cmd;'
– terdon
Aug 22 '12 at 12:56
I tried both and neither worked, is there any difference?
– dutt
Aug 22 '12 at 13:41
1
1
Running the script does not change the environment by default. You've misdiagnosed the problem.
– Ignacio Vazquez-Abrams
Aug 22 '12 at 11:29
Running the script does not change the environment by default. You've misdiagnosed the problem.
– Ignacio Vazquez-Abrams
Aug 22 '12 at 11:29
1
1
Be sure to
export
the variables needed by the script before running it.– choroba
Aug 22 '12 at 11:31
Be sure to
export
the variables needed by the script before running it.– choroba
Aug 22 '12 at 11:31
Ingacio: Hmkay, then I don't know. I do exec "$command" in the script and it doesn't work, the exact same command in the terminal work. choroba: I'm not sure which variables it is :/ it's a big enterprise environment with scripts and variables all over the place.
– dutt
Aug 22 '12 at 11:36
Ingacio: Hmkay, then I don't know. I do exec "$command" in the script and it doesn't work, the exact same command in the terminal work. choroba: I'm not sure which variables it is :/ it's a big enterprise environment with scripts and variables all over the place.
– dutt
Aug 22 '12 at 11:36
Is there some special reason you are using exec? Why not just write the command directly? e.g.: `cmd=ls; $cmd;'
– terdon
Aug 22 '12 at 12:56
Is there some special reason you are using exec? Why not just write the command directly? e.g.: `cmd=ls; $cmd;'
– terdon
Aug 22 '12 at 12:56
I tried both and neither worked, is there any difference?
– dutt
Aug 22 '12 at 13:41
I tried both and neither worked, is there any difference?
– dutt
Aug 22 '12 at 13:41
add a comment |
3 Answers
3
active
oldest
votes
Put the command in an array instead.
arr=(foo bar baz quux)
exec "${arr[@]}"
add a comment |
Type set -o allexport
at the top of your bash script. This should automatically export all variables you define. (Do note that this is considered bad style; a better thing would be to just export the proper variables).
add a comment |
If all the command should be ran from the same dir you can use
cd /d $Location
at the beginning of the script to set the location for the commands to be ran.
cd -- This is the change directory command.
/d -- This switch makes cd change both drive and directory at once.
if you wanted the command to be ran from say D:scripts
with out the /d you would need to
D:
cd scripts
but with the above command you can do it in one line
cd /d D:scripts
-Edit again-
My apologies, it clearly says BASH but i read it at BATCH for some reason.
will be more careful in the future.
1
Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what/d
is supposed to mean? Aren't you confusingcd
ofcmd.exe
with the one ofbash
? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).
– Kamil Maciorowski
Dec 18 '18 at 6:34
@KamilMaciorowski - edited to explain more.
– jesse
Dec 18 '18 at 13:29
1
The question is tagged bash but your answer explainscd
ofcmd.exe
.cd
in Bash will treat/d
as a path. On the other hand$Location
looks like a Bash variable and I guesscmd.exe
won't understand it. My downvote stands because the answer confuses two differentcd
commands and it's therefore not useful.
– Kamil Maciorowski
Dec 18 '18 at 13:37
One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers tocd
ofcmd.exe
and keep what refers tocd
ofbash
(not the other way around).
– Kamil Maciorowski
Dec 18 '18 at 13:49
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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%2fsuperuser.com%2fquestions%2f464811%2fkeep-environment-when-executing-in-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Put the command in an array instead.
arr=(foo bar baz quux)
exec "${arr[@]}"
add a comment |
Put the command in an array instead.
arr=(foo bar baz quux)
exec "${arr[@]}"
add a comment |
Put the command in an array instead.
arr=(foo bar baz quux)
exec "${arr[@]}"
Put the command in an array instead.
arr=(foo bar baz quux)
exec "${arr[@]}"
answered Aug 22 '12 at 11:40
Ignacio Vazquez-AbramsIgnacio Vazquez-Abrams
95.6k6150209
95.6k6150209
add a comment |
add a comment |
Type set -o allexport
at the top of your bash script. This should automatically export all variables you define. (Do note that this is considered bad style; a better thing would be to just export the proper variables).
add a comment |
Type set -o allexport
at the top of your bash script. This should automatically export all variables you define. (Do note that this is considered bad style; a better thing would be to just export the proper variables).
add a comment |
Type set -o allexport
at the top of your bash script. This should automatically export all variables you define. (Do note that this is considered bad style; a better thing would be to just export the proper variables).
Type set -o allexport
at the top of your bash script. This should automatically export all variables you define. (Do note that this is considered bad style; a better thing would be to just export the proper variables).
answered Aug 23 '12 at 19:32
srcsrc
1816
1816
add a comment |
add a comment |
If all the command should be ran from the same dir you can use
cd /d $Location
at the beginning of the script to set the location for the commands to be ran.
cd -- This is the change directory command.
/d -- This switch makes cd change both drive and directory at once.
if you wanted the command to be ran from say D:scripts
with out the /d you would need to
D:
cd scripts
but with the above command you can do it in one line
cd /d D:scripts
-Edit again-
My apologies, it clearly says BASH but i read it at BATCH for some reason.
will be more careful in the future.
1
Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what/d
is supposed to mean? Aren't you confusingcd
ofcmd.exe
with the one ofbash
? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).
– Kamil Maciorowski
Dec 18 '18 at 6:34
@KamilMaciorowski - edited to explain more.
– jesse
Dec 18 '18 at 13:29
1
The question is tagged bash but your answer explainscd
ofcmd.exe
.cd
in Bash will treat/d
as a path. On the other hand$Location
looks like a Bash variable and I guesscmd.exe
won't understand it. My downvote stands because the answer confuses two differentcd
commands and it's therefore not useful.
– Kamil Maciorowski
Dec 18 '18 at 13:37
One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers tocd
ofcmd.exe
and keep what refers tocd
ofbash
(not the other way around).
– Kamil Maciorowski
Dec 18 '18 at 13:49
add a comment |
If all the command should be ran from the same dir you can use
cd /d $Location
at the beginning of the script to set the location for the commands to be ran.
cd -- This is the change directory command.
/d -- This switch makes cd change both drive and directory at once.
if you wanted the command to be ran from say D:scripts
with out the /d you would need to
D:
cd scripts
but with the above command you can do it in one line
cd /d D:scripts
-Edit again-
My apologies, it clearly says BASH but i read it at BATCH for some reason.
will be more careful in the future.
1
Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what/d
is supposed to mean? Aren't you confusingcd
ofcmd.exe
with the one ofbash
? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).
– Kamil Maciorowski
Dec 18 '18 at 6:34
@KamilMaciorowski - edited to explain more.
– jesse
Dec 18 '18 at 13:29
1
The question is tagged bash but your answer explainscd
ofcmd.exe
.cd
in Bash will treat/d
as a path. On the other hand$Location
looks like a Bash variable and I guesscmd.exe
won't understand it. My downvote stands because the answer confuses two differentcd
commands and it's therefore not useful.
– Kamil Maciorowski
Dec 18 '18 at 13:37
One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers tocd
ofcmd.exe
and keep what refers tocd
ofbash
(not the other way around).
– Kamil Maciorowski
Dec 18 '18 at 13:49
add a comment |
If all the command should be ran from the same dir you can use
cd /d $Location
at the beginning of the script to set the location for the commands to be ran.
cd -- This is the change directory command.
/d -- This switch makes cd change both drive and directory at once.
if you wanted the command to be ran from say D:scripts
with out the /d you would need to
D:
cd scripts
but with the above command you can do it in one line
cd /d D:scripts
-Edit again-
My apologies, it clearly says BASH but i read it at BATCH for some reason.
will be more careful in the future.
If all the command should be ran from the same dir you can use
cd /d $Location
at the beginning of the script to set the location for the commands to be ran.
cd -- This is the change directory command.
/d -- This switch makes cd change both drive and directory at once.
if you wanted the command to be ran from say D:scripts
with out the /d you would need to
D:
cd scripts
but with the above command you can do it in one line
cd /d D:scripts
-Edit again-
My apologies, it clearly says BASH but i read it at BATCH for some reason.
will be more careful in the future.
edited Dec 18 '18 at 14:03
answered Dec 17 '18 at 20:53
jessejesse
1011
1011
1
Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what/d
is supposed to mean? Aren't you confusingcd
ofcmd.exe
with the one ofbash
? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).
– Kamil Maciorowski
Dec 18 '18 at 6:34
@KamilMaciorowski - edited to explain more.
– jesse
Dec 18 '18 at 13:29
1
The question is tagged bash but your answer explainscd
ofcmd.exe
.cd
in Bash will treat/d
as a path. On the other hand$Location
looks like a Bash variable and I guesscmd.exe
won't understand it. My downvote stands because the answer confuses two differentcd
commands and it's therefore not useful.
– Kamil Maciorowski
Dec 18 '18 at 13:37
One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers tocd
ofcmd.exe
and keep what refers tocd
ofbash
(not the other way around).
– Kamil Maciorowski
Dec 18 '18 at 13:49
add a comment |
1
Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what/d
is supposed to mean? Aren't you confusingcd
ofcmd.exe
with the one ofbash
? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).
– Kamil Maciorowski
Dec 18 '18 at 6:34
@KamilMaciorowski - edited to explain more.
– jesse
Dec 18 '18 at 13:29
1
The question is tagged bash but your answer explainscd
ofcmd.exe
.cd
in Bash will treat/d
as a path. On the other hand$Location
looks like a Bash variable and I guesscmd.exe
won't understand it. My downvote stands because the answer confuses two differentcd
commands and it's therefore not useful.
– Kamil Maciorowski
Dec 18 '18 at 13:37
One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers tocd
ofcmd.exe
and keep what refers tocd
ofbash
(not the other way around).
– Kamil Maciorowski
Dec 18 '18 at 13:49
1
1
Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what
/d
is supposed to mean? Aren't you confusing cd
of cmd.exe
with the one of bash
? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).– Kamil Maciorowski
Dec 18 '18 at 6:34
Downvoted because this answer is obscure, probably you can save it though. Could you explain (by editing) what
/d
is supposed to mean? Aren't you confusing cd
of cmd.exe
with the one of bash
? Please notify me (@KamilMaciorowski) in a comment to get my attention. I will review the improved answer and I will consider revoking the downvote. (Deleting the answer is also an option, in case you recognize it cannot be improved; but I hope it will be improved).– Kamil Maciorowski
Dec 18 '18 at 6:34
@KamilMaciorowski - edited to explain more.
– jesse
Dec 18 '18 at 13:29
@KamilMaciorowski - edited to explain more.
– jesse
Dec 18 '18 at 13:29
1
1
The question is tagged bash but your answer explains
cd
of cmd.exe
. cd
in Bash will treat /d
as a path. On the other hand $Location
looks like a Bash variable and I guess cmd.exe
won't understand it. My downvote stands because the answer confuses two different cd
commands and it's therefore not useful.– Kamil Maciorowski
Dec 18 '18 at 13:37
The question is tagged bash but your answer explains
cd
of cmd.exe
. cd
in Bash will treat /d
as a path. On the other hand $Location
looks like a Bash variable and I guess cmd.exe
won't understand it. My downvote stands because the answer confuses two different cd
commands and it's therefore not useful.– Kamil Maciorowski
Dec 18 '18 at 13:37
One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers to
cd
of cmd.exe
and keep what refers to cd
of bash
(not the other way around).– Kamil Maciorowski
Dec 18 '18 at 13:49
One more thing: please note any answer should match the question; so the right thing to save the answer is to drop everything that refers to
cd
of cmd.exe
and keep what refers to cd
of bash
(not the other way around).– Kamil Maciorowski
Dec 18 '18 at 13:49
add a comment |
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f464811%2fkeep-environment-when-executing-in-script%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
Running the script does not change the environment by default. You've misdiagnosed the problem.
– Ignacio Vazquez-Abrams
Aug 22 '12 at 11:29
1
Be sure to
export
the variables needed by the script before running it.– choroba
Aug 22 '12 at 11:31
Ingacio: Hmkay, then I don't know. I do exec "$command" in the script and it doesn't work, the exact same command in the terminal work. choroba: I'm not sure which variables it is :/ it's a big enterprise environment with scripts and variables all over the place.
– dutt
Aug 22 '12 at 11:36
Is there some special reason you are using exec? Why not just write the command directly? e.g.: `cmd=ls; $cmd;'
– terdon
Aug 22 '12 at 12:56
I tried both and neither worked, is there any difference?
– dutt
Aug 22 '12 at 13:41