script file not found when using source
I have a bash script in a file named reach.sh.
This file is given exe rights using chmod 755 /Users/vb/Documents/util/bash/reach.sh.
I then created an alias using alias reach='/Users/vb/Documents/util/bash/reach.sh'
So far this works great.
It happens that I need to run this script in my current process, so theoretically I would need to add . or source before my script path.
So I now have alias reach='source /Users/vb/Documents/util/bash/reach.sh'
At this point when I run my alias reach, the script is failing.
Error /Users/vb/Documents/util/bash/reach.sh:7: = not found
Line 7 if [ "$1" == "cr" ] || [ "$1" == "c" ]; then
Full script
#!/bin/bash
# env
REACH_ROOT="/Users/vb/Documents/bitbucket/fork/self"
# process
if [ "$1" == "cr" ] || [ "$1" == "c" ]; then
echo -e "Redirection to subfolder"
cd ${REACH_ROOT}/src/cr
pwd
else
echo -e "Redirection to root folder"
cd ${REACH_ROOT}
pwd
fi
Any idea what I could be missing ?
bash
|
show 12 more comments
I have a bash script in a file named reach.sh.
This file is given exe rights using chmod 755 /Users/vb/Documents/util/bash/reach.sh.
I then created an alias using alias reach='/Users/vb/Documents/util/bash/reach.sh'
So far this works great.
It happens that I need to run this script in my current process, so theoretically I would need to add . or source before my script path.
So I now have alias reach='source /Users/vb/Documents/util/bash/reach.sh'
At this point when I run my alias reach, the script is failing.
Error /Users/vb/Documents/util/bash/reach.sh:7: = not found
Line 7 if [ "$1" == "cr" ] || [ "$1" == "c" ]; then
Full script
#!/bin/bash
# env
REACH_ROOT="/Users/vb/Documents/bitbucket/fork/self"
# process
if [ "$1" == "cr" ] || [ "$1" == "c" ]; then
echo -e "Redirection to subfolder"
cd ${REACH_ROOT}/src/cr
pwd
else
echo -e "Redirection to root folder"
cd ${REACH_ROOT}
pwd
fi
Any idea what I could be missing ?
bash
1
Seems like the script is still found, but inside the script (at line 16 as the:16denotes) there is an error. Can you show us your script? Also remove thecmdtag.bashis notcmd.
– Socowi
Nov 15 '18 at 21:43
3
That said, I can't reproduce your error using any shell I have lying around. What happens if you replace==with=?
– chepner
Nov 15 '18 at 22:05
1
"It happens that I need to run this script in my current process" - what is your current process? Is it a bash shell or something else?
– melpomene
Nov 15 '18 at 22:08
4
==is a bash extension, you shouldn't use it if you need to run the script in another shell.
– Barmar
Nov 15 '18 at 22:13
3
If you want to check portability of a script between shells, change the shebang to/bin/shand paste it into shellcheck.net. It warns about the=
– Barmar
Nov 15 '18 at 22:17
|
show 12 more comments
I have a bash script in a file named reach.sh.
This file is given exe rights using chmod 755 /Users/vb/Documents/util/bash/reach.sh.
I then created an alias using alias reach='/Users/vb/Documents/util/bash/reach.sh'
So far this works great.
It happens that I need to run this script in my current process, so theoretically I would need to add . or source before my script path.
So I now have alias reach='source /Users/vb/Documents/util/bash/reach.sh'
At this point when I run my alias reach, the script is failing.
Error /Users/vb/Documents/util/bash/reach.sh:7: = not found
Line 7 if [ "$1" == "cr" ] || [ "$1" == "c" ]; then
Full script
#!/bin/bash
# env
REACH_ROOT="/Users/vb/Documents/bitbucket/fork/self"
# process
if [ "$1" == "cr" ] || [ "$1" == "c" ]; then
echo -e "Redirection to subfolder"
cd ${REACH_ROOT}/src/cr
pwd
else
echo -e "Redirection to root folder"
cd ${REACH_ROOT}
pwd
fi
Any idea what I could be missing ?
bash
I have a bash script in a file named reach.sh.
This file is given exe rights using chmod 755 /Users/vb/Documents/util/bash/reach.sh.
I then created an alias using alias reach='/Users/vb/Documents/util/bash/reach.sh'
So far this works great.
It happens that I need to run this script in my current process, so theoretically I would need to add . or source before my script path.
So I now have alias reach='source /Users/vb/Documents/util/bash/reach.sh'
At this point when I run my alias reach, the script is failing.
Error /Users/vb/Documents/util/bash/reach.sh:7: = not found
Line 7 if [ "$1" == "cr" ] || [ "$1" == "c" ]; then
Full script
#!/bin/bash
# env
REACH_ROOT="/Users/vb/Documents/bitbucket/fork/self"
# process
if [ "$1" == "cr" ] || [ "$1" == "c" ]; then
echo -e "Redirection to subfolder"
cd ${REACH_ROOT}/src/cr
pwd
else
echo -e "Redirection to root folder"
cd ${REACH_ROOT}
pwd
fi
Any idea what I could be missing ?
bash
bash
edited Nov 15 '18 at 22:02
vbuzze
asked Nov 15 '18 at 21:39
vbuzzevbuzze
371414
371414
1
Seems like the script is still found, but inside the script (at line 16 as the:16denotes) there is an error. Can you show us your script? Also remove thecmdtag.bashis notcmd.
– Socowi
Nov 15 '18 at 21:43
3
That said, I can't reproduce your error using any shell I have lying around. What happens if you replace==with=?
– chepner
Nov 15 '18 at 22:05
1
"It happens that I need to run this script in my current process" - what is your current process? Is it a bash shell or something else?
– melpomene
Nov 15 '18 at 22:08
4
==is a bash extension, you shouldn't use it if you need to run the script in another shell.
– Barmar
Nov 15 '18 at 22:13
3
If you want to check portability of a script between shells, change the shebang to/bin/shand paste it into shellcheck.net. It warns about the=
– Barmar
Nov 15 '18 at 22:17
|
show 12 more comments
1
Seems like the script is still found, but inside the script (at line 16 as the:16denotes) there is an error. Can you show us your script? Also remove thecmdtag.bashis notcmd.
– Socowi
Nov 15 '18 at 21:43
3
That said, I can't reproduce your error using any shell I have lying around. What happens if you replace==with=?
– chepner
Nov 15 '18 at 22:05
1
"It happens that I need to run this script in my current process" - what is your current process? Is it a bash shell or something else?
– melpomene
Nov 15 '18 at 22:08
4
==is a bash extension, you shouldn't use it if you need to run the script in another shell.
– Barmar
Nov 15 '18 at 22:13
3
If you want to check portability of a script between shells, change the shebang to/bin/shand paste it into shellcheck.net. It warns about the=
– Barmar
Nov 15 '18 at 22:17
1
1
Seems like the script is still found, but inside the script (at line 16 as the
:16 denotes) there is an error. Can you show us your script? Also remove the cmd tag. bash is not cmd.– Socowi
Nov 15 '18 at 21:43
Seems like the script is still found, but inside the script (at line 16 as the
:16 denotes) there is an error. Can you show us your script? Also remove the cmd tag. bash is not cmd.– Socowi
Nov 15 '18 at 21:43
3
3
That said, I can't reproduce your error using any shell I have lying around. What happens if you replace
== with =?– chepner
Nov 15 '18 at 22:05
That said, I can't reproduce your error using any shell I have lying around. What happens if you replace
== with =?– chepner
Nov 15 '18 at 22:05
1
1
"It happens that I need to run this script in my current process" - what is your current process? Is it a bash shell or something else?
– melpomene
Nov 15 '18 at 22:08
"It happens that I need to run this script in my current process" - what is your current process? Is it a bash shell or something else?
– melpomene
Nov 15 '18 at 22:08
4
4
== is a bash extension, you shouldn't use it if you need to run the script in another shell.– Barmar
Nov 15 '18 at 22:13
== is a bash extension, you shouldn't use it if you need to run the script in another shell.– Barmar
Nov 15 '18 at 22:13
3
3
If you want to check portability of a script between shells, change the shebang to
/bin/sh and paste it into shellcheck.net. It warns about the =– Barmar
Nov 15 '18 at 22:17
If you want to check portability of a script between shells, change the shebang to
/bin/sh and paste it into shellcheck.net. It warns about the =– Barmar
Nov 15 '18 at 22:17
|
show 12 more comments
2 Answers
2
active
oldest
votes
I'm running my script in zsh which is not a bash shell, so when I force it to run in my current process it runs in a zsh shell and does not recognize bash commands anymore.
Script failure has nothing to do with the Bash/ZSH, its the incorrect use ofifcondition. You can't use[surrounding for string comparison. Try putting[[and that should work.if [[ "$1" == "cr" ]] || [[ "$1" == "c" ]]; thenSingle[only work if you use-eqstyle comparison with numbers and whatnot. This happens with Mac version of BSD Bash and not with AT&T version of Bash used by Ubuntu and other enterprise Linux systems.
– Praveen Premaratne
Nov 15 '18 at 22:36
@praveen: not so. You can do string comparison with the[command, but you need to spell the comparison operators correctly. Equality is spelled=, not==.
– rici
Nov 15 '18 at 22:52
add a comment |
In your question, you say "It happens that I need to run this script in my current process", so I'm wondering why you are using source at all. Just run the script. Observe:
bash-script.sh
#!/bin/bash
if [ "$1" == "aaa" ]; then
echo "AAA"
fi
zsh-script.sh
#!/bin/zsh
echo "try a ..."
./bash-script.sh a
echo "try aaa ..."
./bash-script.sh aaa
echo "try b ..."
./bash-script.sh b
output from ./zsh-script.sh
try a ...
try aaa ...
AAA
try b ...
If, in zsh-script.sh, I put source in front of each ./bash-script.sh, I do get the behavior you described in your question.
But, if you just need to "run this script in my current process", well, then ... just run it.
source tries to read a file as lines to be interpreted by the current shell, which is zsh as you have said. But simply running it, causes the first line (the #!/bin/bash "shebang" line) to start a new shell that interprets the lines itself. That will totally resolve the use of bash syntax from within a zsh context.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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%2fstackoverflow.com%2fquestions%2f53328262%2fscript-file-not-found-when-using-source%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
I'm running my script in zsh which is not a bash shell, so when I force it to run in my current process it runs in a zsh shell and does not recognize bash commands anymore.
Script failure has nothing to do with the Bash/ZSH, its the incorrect use ofifcondition. You can't use[surrounding for string comparison. Try putting[[and that should work.if [[ "$1" == "cr" ]] || [[ "$1" == "c" ]]; thenSingle[only work if you use-eqstyle comparison with numbers and whatnot. This happens with Mac version of BSD Bash and not with AT&T version of Bash used by Ubuntu and other enterprise Linux systems.
– Praveen Premaratne
Nov 15 '18 at 22:36
@praveen: not so. You can do string comparison with the[command, but you need to spell the comparison operators correctly. Equality is spelled=, not==.
– rici
Nov 15 '18 at 22:52
add a comment |
I'm running my script in zsh which is not a bash shell, so when I force it to run in my current process it runs in a zsh shell and does not recognize bash commands anymore.
Script failure has nothing to do with the Bash/ZSH, its the incorrect use ofifcondition. You can't use[surrounding for string comparison. Try putting[[and that should work.if [[ "$1" == "cr" ]] || [[ "$1" == "c" ]]; thenSingle[only work if you use-eqstyle comparison with numbers and whatnot. This happens with Mac version of BSD Bash and not with AT&T version of Bash used by Ubuntu and other enterprise Linux systems.
– Praveen Premaratne
Nov 15 '18 at 22:36
@praveen: not so. You can do string comparison with the[command, but you need to spell the comparison operators correctly. Equality is spelled=, not==.
– rici
Nov 15 '18 at 22:52
add a comment |
I'm running my script in zsh which is not a bash shell, so when I force it to run in my current process it runs in a zsh shell and does not recognize bash commands anymore.
I'm running my script in zsh which is not a bash shell, so when I force it to run in my current process it runs in a zsh shell and does not recognize bash commands anymore.
answered Nov 15 '18 at 22:16
vbuzzevbuzze
371414
371414
Script failure has nothing to do with the Bash/ZSH, its the incorrect use ofifcondition. You can't use[surrounding for string comparison. Try putting[[and that should work.if [[ "$1" == "cr" ]] || [[ "$1" == "c" ]]; thenSingle[only work if you use-eqstyle comparison with numbers and whatnot. This happens with Mac version of BSD Bash and not with AT&T version of Bash used by Ubuntu and other enterprise Linux systems.
– Praveen Premaratne
Nov 15 '18 at 22:36
@praveen: not so. You can do string comparison with the[command, but you need to spell the comparison operators correctly. Equality is spelled=, not==.
– rici
Nov 15 '18 at 22:52
add a comment |
Script failure has nothing to do with the Bash/ZSH, its the incorrect use ofifcondition. You can't use[surrounding for string comparison. Try putting[[and that should work.if [[ "$1" == "cr" ]] || [[ "$1" == "c" ]]; thenSingle[only work if you use-eqstyle comparison with numbers and whatnot. This happens with Mac version of BSD Bash and not with AT&T version of Bash used by Ubuntu and other enterprise Linux systems.
– Praveen Premaratne
Nov 15 '18 at 22:36
@praveen: not so. You can do string comparison with the[command, but you need to spell the comparison operators correctly. Equality is spelled=, not==.
– rici
Nov 15 '18 at 22:52
Script failure has nothing to do with the Bash/ZSH, its the incorrect use of
if condition. You can't use [ surrounding for string comparison. Try putting [[ and that should work. if [[ "$1" == "cr" ]] || [[ "$1" == "c" ]]; then Single [ only work if you use -eq style comparison with numbers and whatnot. This happens with Mac version of BSD Bash and not with AT&T version of Bash used by Ubuntu and other enterprise Linux systems.– Praveen Premaratne
Nov 15 '18 at 22:36
Script failure has nothing to do with the Bash/ZSH, its the incorrect use of
if condition. You can't use [ surrounding for string comparison. Try putting [[ and that should work. if [[ "$1" == "cr" ]] || [[ "$1" == "c" ]]; then Single [ only work if you use -eq style comparison with numbers and whatnot. This happens with Mac version of BSD Bash and not with AT&T version of Bash used by Ubuntu and other enterprise Linux systems.– Praveen Premaratne
Nov 15 '18 at 22:36
@praveen: not so. You can do string comparison with the
[ command, but you need to spell the comparison operators correctly. Equality is spelled =, not ==.– rici
Nov 15 '18 at 22:52
@praveen: not so. You can do string comparison with the
[ command, but you need to spell the comparison operators correctly. Equality is spelled =, not ==.– rici
Nov 15 '18 at 22:52
add a comment |
In your question, you say "It happens that I need to run this script in my current process", so I'm wondering why you are using source at all. Just run the script. Observe:
bash-script.sh
#!/bin/bash
if [ "$1" == "aaa" ]; then
echo "AAA"
fi
zsh-script.sh
#!/bin/zsh
echo "try a ..."
./bash-script.sh a
echo "try aaa ..."
./bash-script.sh aaa
echo "try b ..."
./bash-script.sh b
output from ./zsh-script.sh
try a ...
try aaa ...
AAA
try b ...
If, in zsh-script.sh, I put source in front of each ./bash-script.sh, I do get the behavior you described in your question.
But, if you just need to "run this script in my current process", well, then ... just run it.
source tries to read a file as lines to be interpreted by the current shell, which is zsh as you have said. But simply running it, causes the first line (the #!/bin/bash "shebang" line) to start a new shell that interprets the lines itself. That will totally resolve the use of bash syntax from within a zsh context.
add a comment |
In your question, you say "It happens that I need to run this script in my current process", so I'm wondering why you are using source at all. Just run the script. Observe:
bash-script.sh
#!/bin/bash
if [ "$1" == "aaa" ]; then
echo "AAA"
fi
zsh-script.sh
#!/bin/zsh
echo "try a ..."
./bash-script.sh a
echo "try aaa ..."
./bash-script.sh aaa
echo "try b ..."
./bash-script.sh b
output from ./zsh-script.sh
try a ...
try aaa ...
AAA
try b ...
If, in zsh-script.sh, I put source in front of each ./bash-script.sh, I do get the behavior you described in your question.
But, if you just need to "run this script in my current process", well, then ... just run it.
source tries to read a file as lines to be interpreted by the current shell, which is zsh as you have said. But simply running it, causes the first line (the #!/bin/bash "shebang" line) to start a new shell that interprets the lines itself. That will totally resolve the use of bash syntax from within a zsh context.
add a comment |
In your question, you say "It happens that I need to run this script in my current process", so I'm wondering why you are using source at all. Just run the script. Observe:
bash-script.sh
#!/bin/bash
if [ "$1" == "aaa" ]; then
echo "AAA"
fi
zsh-script.sh
#!/bin/zsh
echo "try a ..."
./bash-script.sh a
echo "try aaa ..."
./bash-script.sh aaa
echo "try b ..."
./bash-script.sh b
output from ./zsh-script.sh
try a ...
try aaa ...
AAA
try b ...
If, in zsh-script.sh, I put source in front of each ./bash-script.sh, I do get the behavior you described in your question.
But, if you just need to "run this script in my current process", well, then ... just run it.
source tries to read a file as lines to be interpreted by the current shell, which is zsh as you have said. But simply running it, causes the first line (the #!/bin/bash "shebang" line) to start a new shell that interprets the lines itself. That will totally resolve the use of bash syntax from within a zsh context.
In your question, you say "It happens that I need to run this script in my current process", so I'm wondering why you are using source at all. Just run the script. Observe:
bash-script.sh
#!/bin/bash
if [ "$1" == "aaa" ]; then
echo "AAA"
fi
zsh-script.sh
#!/bin/zsh
echo "try a ..."
./bash-script.sh a
echo "try aaa ..."
./bash-script.sh aaa
echo "try b ..."
./bash-script.sh b
output from ./zsh-script.sh
try a ...
try aaa ...
AAA
try b ...
If, in zsh-script.sh, I put source in front of each ./bash-script.sh, I do get the behavior you described in your question.
But, if you just need to "run this script in my current process", well, then ... just run it.
source tries to read a file as lines to be interpreted by the current shell, which is zsh as you have said. But simply running it, causes the first line (the #!/bin/bash "shebang" line) to start a new shell that interprets the lines itself. That will totally resolve the use of bash syntax from within a zsh context.
edited Nov 15 '18 at 23:57
answered Nov 15 '18 at 22:52
landru27landru27
787314
787314
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53328262%2fscript-file-not-found-when-using-source%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
Seems like the script is still found, but inside the script (at line 16 as the
:16denotes) there is an error. Can you show us your script? Also remove thecmdtag.bashis notcmd.– Socowi
Nov 15 '18 at 21:43
3
That said, I can't reproduce your error using any shell I have lying around. What happens if you replace
==with=?– chepner
Nov 15 '18 at 22:05
1
"It happens that I need to run this script in my current process" - what is your current process? Is it a bash shell or something else?
– melpomene
Nov 15 '18 at 22:08
4
==is a bash extension, you shouldn't use it if you need to run the script in another shell.– Barmar
Nov 15 '18 at 22:13
3
If you want to check portability of a script between shells, change the shebang to
/bin/shand paste it into shellcheck.net. It warns about the=– Barmar
Nov 15 '18 at 22:17