Return from emulate bash unsets zsh opts
So I have some setup scripts at work, which only work with bash (because they use e.g. == or $BASH_SOURCE).
It works to source these scripts using zsh's emulate command:
emulate bash
source setupscipt.bash
emulate zsh
But this resets my zsh opts like "autocd" (set in .zshrc).
How should I start and quit emulation of bash, so that my opts stay as they are?
terminal zsh emulation
add a comment |
So I have some setup scripts at work, which only work with bash (because they use e.g. == or $BASH_SOURCE).
It works to source these scripts using zsh's emulate command:
emulate bash
source setupscipt.bash
emulate zsh
But this resets my zsh opts like "autocd" (set in .zshrc).
How should I start and quit emulation of bash, so that my opts stay as they are?
terminal zsh emulation
add a comment |
So I have some setup scripts at work, which only work with bash (because they use e.g. == or $BASH_SOURCE).
It works to source these scripts using zsh's emulate command:
emulate bash
source setupscipt.bash
emulate zsh
But this resets my zsh opts like "autocd" (set in .zshrc).
How should I start and quit emulation of bash, so that my opts stay as they are?
terminal zsh emulation
So I have some setup scripts at work, which only work with bash (because they use e.g. == or $BASH_SOURCE).
It works to source these scripts using zsh's emulate command:
emulate bash
source setupscipt.bash
emulate zsh
But this resets my zsh opts like "autocd" (set in .zshrc).
How should I start and quit emulation of bash, so that my opts stay as they are?
terminal zsh emulation
terminal zsh emulation
asked Mar 23 '18 at 15:08
Jounathaen
307111
307111
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Zefram answered a similar question on the zsh mailing list over 20 years ago:
The solution is to use a function -- let's call it source_bash -- to source you bash script:
function source_bash {
emulate -L bash
builtin source "$@"
}
The trick is the option localoptions (set by the -L parameter of the emulate call), which is described as follows in the man page:
LOCAL_OPTIONS If this option is set at the point of return from a
shell function, most options (including this one) which were in force
upon entry to the function are restored; options that are not
restored are PRIVILEGED and RESTRICTED. Otherwise, only this option,
and the
LOCAL_LOOPS, XTRACE and PRINT_EXIT_VALUE options are restored. Hence if this is explicitly unset by a shell function
the other
options in force at the point of return will remain so. A shell function can also guarantee itself a known shell configuration
with
a formulation like `emulate -L zsh'; the -L activates LOCAL_OPTIONS.
Put this function to your ~/.zshrcand you'll only need to do a
$ source_bash setupscipt.bash
when necessary.
Cool trick, but just to be shure: Environment variables which are set in the setupscript will then also be set in the parenting zsh?
– Jounathaen
Mar 23 '18 at 18:50
2
@Jounathaen: Yes. Thelocaloptionsoption only restores the zsh options; the environment keeps unchanged. But please try, if it's working for your setup exactly as you want it.
– mpy
Mar 23 '18 at 18:59
1
As mentioned in the last line of that man page quote, one can use the shorteremulate -L bashinstead of havingsetopt localoptionson its own line. :)
– ZeroKnight
Mar 25 '18 at 3:47
@ZeroKnight: Yes, you're right. I edited the function accordingly.
– mpy
Mar 25 '18 at 8:12
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%2f1307205%2freturn-from-emulate-bash-unsets-zsh-opts%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
Zefram answered a similar question on the zsh mailing list over 20 years ago:
The solution is to use a function -- let's call it source_bash -- to source you bash script:
function source_bash {
emulate -L bash
builtin source "$@"
}
The trick is the option localoptions (set by the -L parameter of the emulate call), which is described as follows in the man page:
LOCAL_OPTIONS If this option is set at the point of return from a
shell function, most options (including this one) which were in force
upon entry to the function are restored; options that are not
restored are PRIVILEGED and RESTRICTED. Otherwise, only this option,
and the
LOCAL_LOOPS, XTRACE and PRINT_EXIT_VALUE options are restored. Hence if this is explicitly unset by a shell function
the other
options in force at the point of return will remain so. A shell function can also guarantee itself a known shell configuration
with
a formulation like `emulate -L zsh'; the -L activates LOCAL_OPTIONS.
Put this function to your ~/.zshrcand you'll only need to do a
$ source_bash setupscipt.bash
when necessary.
Cool trick, but just to be shure: Environment variables which are set in the setupscript will then also be set in the parenting zsh?
– Jounathaen
Mar 23 '18 at 18:50
2
@Jounathaen: Yes. Thelocaloptionsoption only restores the zsh options; the environment keeps unchanged. But please try, if it's working for your setup exactly as you want it.
– mpy
Mar 23 '18 at 18:59
1
As mentioned in the last line of that man page quote, one can use the shorteremulate -L bashinstead of havingsetopt localoptionson its own line. :)
– ZeroKnight
Mar 25 '18 at 3:47
@ZeroKnight: Yes, you're right. I edited the function accordingly.
– mpy
Mar 25 '18 at 8:12
add a comment |
Zefram answered a similar question on the zsh mailing list over 20 years ago:
The solution is to use a function -- let's call it source_bash -- to source you bash script:
function source_bash {
emulate -L bash
builtin source "$@"
}
The trick is the option localoptions (set by the -L parameter of the emulate call), which is described as follows in the man page:
LOCAL_OPTIONS If this option is set at the point of return from a
shell function, most options (including this one) which were in force
upon entry to the function are restored; options that are not
restored are PRIVILEGED and RESTRICTED. Otherwise, only this option,
and the
LOCAL_LOOPS, XTRACE and PRINT_EXIT_VALUE options are restored. Hence if this is explicitly unset by a shell function
the other
options in force at the point of return will remain so. A shell function can also guarantee itself a known shell configuration
with
a formulation like `emulate -L zsh'; the -L activates LOCAL_OPTIONS.
Put this function to your ~/.zshrcand you'll only need to do a
$ source_bash setupscipt.bash
when necessary.
Cool trick, but just to be shure: Environment variables which are set in the setupscript will then also be set in the parenting zsh?
– Jounathaen
Mar 23 '18 at 18:50
2
@Jounathaen: Yes. Thelocaloptionsoption only restores the zsh options; the environment keeps unchanged. But please try, if it's working for your setup exactly as you want it.
– mpy
Mar 23 '18 at 18:59
1
As mentioned in the last line of that man page quote, one can use the shorteremulate -L bashinstead of havingsetopt localoptionson its own line. :)
– ZeroKnight
Mar 25 '18 at 3:47
@ZeroKnight: Yes, you're right. I edited the function accordingly.
– mpy
Mar 25 '18 at 8:12
add a comment |
Zefram answered a similar question on the zsh mailing list over 20 years ago:
The solution is to use a function -- let's call it source_bash -- to source you bash script:
function source_bash {
emulate -L bash
builtin source "$@"
}
The trick is the option localoptions (set by the -L parameter of the emulate call), which is described as follows in the man page:
LOCAL_OPTIONS If this option is set at the point of return from a
shell function, most options (including this one) which were in force
upon entry to the function are restored; options that are not
restored are PRIVILEGED and RESTRICTED. Otherwise, only this option,
and the
LOCAL_LOOPS, XTRACE and PRINT_EXIT_VALUE options are restored. Hence if this is explicitly unset by a shell function
the other
options in force at the point of return will remain so. A shell function can also guarantee itself a known shell configuration
with
a formulation like `emulate -L zsh'; the -L activates LOCAL_OPTIONS.
Put this function to your ~/.zshrcand you'll only need to do a
$ source_bash setupscipt.bash
when necessary.
Zefram answered a similar question on the zsh mailing list over 20 years ago:
The solution is to use a function -- let's call it source_bash -- to source you bash script:
function source_bash {
emulate -L bash
builtin source "$@"
}
The trick is the option localoptions (set by the -L parameter of the emulate call), which is described as follows in the man page:
LOCAL_OPTIONS If this option is set at the point of return from a
shell function, most options (including this one) which were in force
upon entry to the function are restored; options that are not
restored are PRIVILEGED and RESTRICTED. Otherwise, only this option,
and the
LOCAL_LOOPS, XTRACE and PRINT_EXIT_VALUE options are restored. Hence if this is explicitly unset by a shell function
the other
options in force at the point of return will remain so. A shell function can also guarantee itself a known shell configuration
with
a formulation like `emulate -L zsh'; the -L activates LOCAL_OPTIONS.
Put this function to your ~/.zshrcand you'll only need to do a
$ source_bash setupscipt.bash
when necessary.
edited Dec 9 '18 at 10:10
answered Mar 23 '18 at 18:32
mpy
17.9k45271
17.9k45271
Cool trick, but just to be shure: Environment variables which are set in the setupscript will then also be set in the parenting zsh?
– Jounathaen
Mar 23 '18 at 18:50
2
@Jounathaen: Yes. Thelocaloptionsoption only restores the zsh options; the environment keeps unchanged. But please try, if it's working for your setup exactly as you want it.
– mpy
Mar 23 '18 at 18:59
1
As mentioned in the last line of that man page quote, one can use the shorteremulate -L bashinstead of havingsetopt localoptionson its own line. :)
– ZeroKnight
Mar 25 '18 at 3:47
@ZeroKnight: Yes, you're right. I edited the function accordingly.
– mpy
Mar 25 '18 at 8:12
add a comment |
Cool trick, but just to be shure: Environment variables which are set in the setupscript will then also be set in the parenting zsh?
– Jounathaen
Mar 23 '18 at 18:50
2
@Jounathaen: Yes. Thelocaloptionsoption only restores the zsh options; the environment keeps unchanged. But please try, if it's working for your setup exactly as you want it.
– mpy
Mar 23 '18 at 18:59
1
As mentioned in the last line of that man page quote, one can use the shorteremulate -L bashinstead of havingsetopt localoptionson its own line. :)
– ZeroKnight
Mar 25 '18 at 3:47
@ZeroKnight: Yes, you're right. I edited the function accordingly.
– mpy
Mar 25 '18 at 8:12
Cool trick, but just to be shure: Environment variables which are set in the setupscript will then also be set in the parenting zsh?
– Jounathaen
Mar 23 '18 at 18:50
Cool trick, but just to be shure: Environment variables which are set in the setupscript will then also be set in the parenting zsh?
– Jounathaen
Mar 23 '18 at 18:50
2
2
@Jounathaen: Yes. The
localoptions option only restores the zsh options; the environment keeps unchanged. But please try, if it's working for your setup exactly as you want it.– mpy
Mar 23 '18 at 18:59
@Jounathaen: Yes. The
localoptions option only restores the zsh options; the environment keeps unchanged. But please try, if it's working for your setup exactly as you want it.– mpy
Mar 23 '18 at 18:59
1
1
As mentioned in the last line of that man page quote, one can use the shorter
emulate -L bash instead of having setopt localoptions on its own line. :)– ZeroKnight
Mar 25 '18 at 3:47
As mentioned in the last line of that man page quote, one can use the shorter
emulate -L bash instead of having setopt localoptions on its own line. :)– ZeroKnight
Mar 25 '18 at 3:47
@ZeroKnight: Yes, you're right. I edited the function accordingly.
– mpy
Mar 25 '18 at 8:12
@ZeroKnight: Yes, you're right. I edited the function accordingly.
– mpy
Mar 25 '18 at 8:12
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f1307205%2freturn-from-emulate-bash-unsets-zsh-opts%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