single and double equals both behaving differently

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
Here's a simple line from a bigger piece of code which is what I am confused about.
if [ $some_line == "text" ]
then
Then I went on to use the same code for another program that I was working on but the code didn't work UNLESS I changed "==" to "=". I've gone through a few threads here that suggests both of them act the same way so it doesn't matter if you use single or double equals.
if [ $some_line = "text" ]
then
So the first piece of code works on server1 but doesn't on server2 UNLESS I change it to "single equals". Both the servers have exactly the same environment.
Can anyone explain?
Thanks!
EDIT - I am running the script as "bash myscript.sh" everytime.
bash
add a comment |Â
up vote
1
down vote
favorite
Here's a simple line from a bigger piece of code which is what I am confused about.
if [ $some_line == "text" ]
then
Then I went on to use the same code for another program that I was working on but the code didn't work UNLESS I changed "==" to "=". I've gone through a few threads here that suggests both of them act the same way so it doesn't matter if you use single or double equals.
if [ $some_line = "text" ]
then
So the first piece of code works on server1 but doesn't on server2 UNLESS I change it to "single equals". Both the servers have exactly the same environment.
Can anyone explain?
Thanks!
EDIT - I am running the script as "bash myscript.sh" everytime.
bash
Please [edit[ your question and i) tell us how you are launching the script (sh script.sh?bash script.sh? Justscript.sh? Something else?) and also show us the output ofls -l /bin/shon both machines.
â terdonâ¦
May 30 at 11:40
1
Please explain "does not work". Did it do something unexpected or produce an error, or something else?
â Kusalananda
May 30 at 11:48
And did you check if you have some special characters in the variable?
â Romeo Ninov
May 30 at 11:49
1
"So the first piece of code works on server1 but doesn't on server2", but "Both the servers have exactly the same environment.". I'd say the premise of the question is obviously wrong. If the environment between the servers were the same, both=and==would behave the same on both of them.
â ilkkachu
May 30 at 11:49
On the contrary, many answers here such as unix.stackexchange.com/a/382012/5132 , unix.stackexchange.com/a/16110/5132 , and unix.stackexchange.com/a/168288/5132 say that it does matter.
â JdeBP
May 30 at 12:19
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Here's a simple line from a bigger piece of code which is what I am confused about.
if [ $some_line == "text" ]
then
Then I went on to use the same code for another program that I was working on but the code didn't work UNLESS I changed "==" to "=". I've gone through a few threads here that suggests both of them act the same way so it doesn't matter if you use single or double equals.
if [ $some_line = "text" ]
then
So the first piece of code works on server1 but doesn't on server2 UNLESS I change it to "single equals". Both the servers have exactly the same environment.
Can anyone explain?
Thanks!
EDIT - I am running the script as "bash myscript.sh" everytime.
bash
Here's a simple line from a bigger piece of code which is what I am confused about.
if [ $some_line == "text" ]
then
Then I went on to use the same code for another program that I was working on but the code didn't work UNLESS I changed "==" to "=". I've gone through a few threads here that suggests both of them act the same way so it doesn't matter if you use single or double equals.
if [ $some_line = "text" ]
then
So the first piece of code works on server1 but doesn't on server2 UNLESS I change it to "single equals". Both the servers have exactly the same environment.
Can anyone explain?
Thanks!
EDIT - I am running the script as "bash myscript.sh" everytime.
bash
edited May 30 at 11:42
asked May 30 at 11:17
Nishant
143
143
Please [edit[ your question and i) tell us how you are launching the script (sh script.sh?bash script.sh? Justscript.sh? Something else?) and also show us the output ofls -l /bin/shon both machines.
â terdonâ¦
May 30 at 11:40
1
Please explain "does not work". Did it do something unexpected or produce an error, or something else?
â Kusalananda
May 30 at 11:48
And did you check if you have some special characters in the variable?
â Romeo Ninov
May 30 at 11:49
1
"So the first piece of code works on server1 but doesn't on server2", but "Both the servers have exactly the same environment.". I'd say the premise of the question is obviously wrong. If the environment between the servers were the same, both=and==would behave the same on both of them.
â ilkkachu
May 30 at 11:49
On the contrary, many answers here such as unix.stackexchange.com/a/382012/5132 , unix.stackexchange.com/a/16110/5132 , and unix.stackexchange.com/a/168288/5132 say that it does matter.
â JdeBP
May 30 at 12:19
add a comment |Â
Please [edit[ your question and i) tell us how you are launching the script (sh script.sh?bash script.sh? Justscript.sh? Something else?) and also show us the output ofls -l /bin/shon both machines.
â terdonâ¦
May 30 at 11:40
1
Please explain "does not work". Did it do something unexpected or produce an error, or something else?
â Kusalananda
May 30 at 11:48
And did you check if you have some special characters in the variable?
â Romeo Ninov
May 30 at 11:49
1
"So the first piece of code works on server1 but doesn't on server2", but "Both the servers have exactly the same environment.". I'd say the premise of the question is obviously wrong. If the environment between the servers were the same, both=and==would behave the same on both of them.
â ilkkachu
May 30 at 11:49
On the contrary, many answers here such as unix.stackexchange.com/a/382012/5132 , unix.stackexchange.com/a/16110/5132 , and unix.stackexchange.com/a/168288/5132 say that it does matter.
â JdeBP
May 30 at 12:19
Please [edit[ your question and i) tell us how you are launching the script (
sh script.sh? bash script.sh? Just script.sh? Something else?) and also show us the output of ls -l /bin/sh on both machines.â terdonâ¦
May 30 at 11:40
Please [edit[ your question and i) tell us how you are launching the script (
sh script.sh? bash script.sh? Just script.sh? Something else?) and also show us the output of ls -l /bin/sh on both machines.â terdonâ¦
May 30 at 11:40
1
1
Please explain "does not work". Did it do something unexpected or produce an error, or something else?
â Kusalananda
May 30 at 11:48
Please explain "does not work". Did it do something unexpected or produce an error, or something else?
â Kusalananda
May 30 at 11:48
And did you check if you have some special characters in the variable?
â Romeo Ninov
May 30 at 11:49
And did you check if you have some special characters in the variable?
â Romeo Ninov
May 30 at 11:49
1
1
"So the first piece of code works on server1 but doesn't on server2", but "Both the servers have exactly the same environment.". I'd say the premise of the question is obviously wrong. If the environment between the servers were the same, both
= and == would behave the same on both of them.â ilkkachu
May 30 at 11:49
"So the first piece of code works on server1 but doesn't on server2", but "Both the servers have exactly the same environment.". I'd say the premise of the question is obviously wrong. If the environment between the servers were the same, both
= and == would behave the same on both of them.â ilkkachu
May 30 at 11:49
On the contrary, many answers here such as unix.stackexchange.com/a/382012/5132 , unix.stackexchange.com/a/16110/5132 , and unix.stackexchange.com/a/168288/5132 say that it does matter.
â JdeBP
May 30 at 12:19
On the contrary, many answers here such as unix.stackexchange.com/a/382012/5132 , unix.stackexchange.com/a/16110/5132 , and unix.stackexchange.com/a/168288/5132 say that it does matter.
â JdeBP
May 30 at 12:19
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
== and = are equivalent inside [ ] tests in bash.
== doesn't work in sh, only =
Are you running both scripts with the same shell?
Example:
$ cat test1
#!/bin/bash
if [ "a" == "a" ];then echo match;fi
$ ./test1
match
$ cat test2
#!/bin/bash
if [ "a" = "a" ];then echo match;fi
$ ./test2
match
$ cat test3
#!/bin/sh
if [ "a" = "a" ];then echo match;fi
$ ./test3
match
$ cat test4
#!/bin/sh
if [ "a" == "a" ];then echo match;fi
$ ./test4
./test4: 2 [: a: unexpected operator
yeah. The only thing that stands out is that this ubuntu server has not been updated since good amount of time due to development reasons. How can I check if both of them are using the same shell / environment while executing the script?
â Nishant
May 30 at 11:23
runps -o comm $$to know which shell you are in orecho $0
â mkmayank
May 30 at 11:23
Since you are using bash, do:echo "$BASH_VERSION"
â Isaac
May 30 at 11:28
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
== and = are equivalent inside [ ] tests in bash.
== doesn't work in sh, only =
Are you running both scripts with the same shell?
Example:
$ cat test1
#!/bin/bash
if [ "a" == "a" ];then echo match;fi
$ ./test1
match
$ cat test2
#!/bin/bash
if [ "a" = "a" ];then echo match;fi
$ ./test2
match
$ cat test3
#!/bin/sh
if [ "a" = "a" ];then echo match;fi
$ ./test3
match
$ cat test4
#!/bin/sh
if [ "a" == "a" ];then echo match;fi
$ ./test4
./test4: 2 [: a: unexpected operator
yeah. The only thing that stands out is that this ubuntu server has not been updated since good amount of time due to development reasons. How can I check if both of them are using the same shell / environment while executing the script?
â Nishant
May 30 at 11:23
runps -o comm $$to know which shell you are in orecho $0
â mkmayank
May 30 at 11:23
Since you are using bash, do:echo "$BASH_VERSION"
â Isaac
May 30 at 11:28
add a comment |Â
up vote
2
down vote
== and = are equivalent inside [ ] tests in bash.
== doesn't work in sh, only =
Are you running both scripts with the same shell?
Example:
$ cat test1
#!/bin/bash
if [ "a" == "a" ];then echo match;fi
$ ./test1
match
$ cat test2
#!/bin/bash
if [ "a" = "a" ];then echo match;fi
$ ./test2
match
$ cat test3
#!/bin/sh
if [ "a" = "a" ];then echo match;fi
$ ./test3
match
$ cat test4
#!/bin/sh
if [ "a" == "a" ];then echo match;fi
$ ./test4
./test4: 2 [: a: unexpected operator
yeah. The only thing that stands out is that this ubuntu server has not been updated since good amount of time due to development reasons. How can I check if both of them are using the same shell / environment while executing the script?
â Nishant
May 30 at 11:23
runps -o comm $$to know which shell you are in orecho $0
â mkmayank
May 30 at 11:23
Since you are using bash, do:echo "$BASH_VERSION"
â Isaac
May 30 at 11:28
add a comment |Â
up vote
2
down vote
up vote
2
down vote
== and = are equivalent inside [ ] tests in bash.
== doesn't work in sh, only =
Are you running both scripts with the same shell?
Example:
$ cat test1
#!/bin/bash
if [ "a" == "a" ];then echo match;fi
$ ./test1
match
$ cat test2
#!/bin/bash
if [ "a" = "a" ];then echo match;fi
$ ./test2
match
$ cat test3
#!/bin/sh
if [ "a" = "a" ];then echo match;fi
$ ./test3
match
$ cat test4
#!/bin/sh
if [ "a" == "a" ];then echo match;fi
$ ./test4
./test4: 2 [: a: unexpected operator
== and = are equivalent inside [ ] tests in bash.
== doesn't work in sh, only =
Are you running both scripts with the same shell?
Example:
$ cat test1
#!/bin/bash
if [ "a" == "a" ];then echo match;fi
$ ./test1
match
$ cat test2
#!/bin/bash
if [ "a" = "a" ];then echo match;fi
$ ./test2
match
$ cat test3
#!/bin/sh
if [ "a" = "a" ];then echo match;fi
$ ./test3
match
$ cat test4
#!/bin/sh
if [ "a" == "a" ];then echo match;fi
$ ./test4
./test4: 2 [: a: unexpected operator
edited May 30 at 11:41
terdonâ¦
122k28226398
122k28226398
answered May 30 at 11:22
rusty shackleford
1,135115
1,135115
yeah. The only thing that stands out is that this ubuntu server has not been updated since good amount of time due to development reasons. How can I check if both of them are using the same shell / environment while executing the script?
â Nishant
May 30 at 11:23
runps -o comm $$to know which shell you are in orecho $0
â mkmayank
May 30 at 11:23
Since you are using bash, do:echo "$BASH_VERSION"
â Isaac
May 30 at 11:28
add a comment |Â
yeah. The only thing that stands out is that this ubuntu server has not been updated since good amount of time due to development reasons. How can I check if both of them are using the same shell / environment while executing the script?
â Nishant
May 30 at 11:23
runps -o comm $$to know which shell you are in orecho $0
â mkmayank
May 30 at 11:23
Since you are using bash, do:echo "$BASH_VERSION"
â Isaac
May 30 at 11:28
yeah. The only thing that stands out is that this ubuntu server has not been updated since good amount of time due to development reasons. How can I check if both of them are using the same shell / environment while executing the script?
â Nishant
May 30 at 11:23
yeah. The only thing that stands out is that this ubuntu server has not been updated since good amount of time due to development reasons. How can I check if both of them are using the same shell / environment while executing the script?
â Nishant
May 30 at 11:23
run
ps -o comm $$ to know which shell you are in or echo $0â mkmayank
May 30 at 11:23
run
ps -o comm $$ to know which shell you are in or echo $0â mkmayank
May 30 at 11:23
Since you are using bash, do:
echo "$BASH_VERSION"â Isaac
May 30 at 11:28
Since you are using bash, do:
echo "$BASH_VERSION"â Isaac
May 30 at 11:28
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%2f446885%2fsingle-and-double-equals-both-behaving-differently%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
Please [edit[ your question and i) tell us how you are launching the script (
sh script.sh?bash script.sh? Justscript.sh? Something else?) and also show us the output ofls -l /bin/shon both machines.â terdonâ¦
May 30 at 11:40
1
Please explain "does not work". Did it do something unexpected or produce an error, or something else?
â Kusalananda
May 30 at 11:48
And did you check if you have some special characters in the variable?
â Romeo Ninov
May 30 at 11:49
1
"So the first piece of code works on server1 but doesn't on server2", but "Both the servers have exactly the same environment.". I'd say the premise of the question is obviously wrong. If the environment between the servers were the same, both
=and==would behave the same on both of them.â ilkkachu
May 30 at 11:49
On the contrary, many answers here such as unix.stackexchange.com/a/382012/5132 , unix.stackexchange.com/a/16110/5132 , and unix.stackexchange.com/a/168288/5132 say that it does matter.
â JdeBP
May 30 at 12:19