why it becomes if [ "" = "1" ]; then when the problem is fixed ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i do not understand why when you fix the problem on line 5, then
Now when the shell performs the expansion it will see:
if [ "" = "1" ]; then
I can't wrap around my head why when the line is changed to if [ "$number" = "1" ]; then the shell will see it to be [ "" = "1" ];
Furthermore, when it is else, shouldn't it be using "" = "1" or the number != "1" so why it is still ["$number" = "1"] which is same as the condition on line 3 ?
What that is telling you is that if the variable $number is empty, and the test is [ $number = 1 ], then when the shell expands it, the test will be...
Code:
[ = 1 ]
... so there is nothing at all to compare to the number 1, which is an error!
When you surround the variable in quotes like this...
Code:
[ "$number" = 1 ]
... and the variable $number is empty, then the shell still sees the empty quotes...
Code:
[ "" = 1 ]
... and the empty quoted string is not nothing, and it can be compared to 1, so the test is valid.
Hope that helps!
Last edited by astrogeek; 09-11-2023 at 12:54 PM.
Reason: tyop
The " " enforce one string, and $ expressions are evaluated+substituted before the string is handed to the [ ]
Compare with ' ' that enforce one string without substitution.
An empty variable without quotes is nothing, not even an empty string.
Redo the exercise with
number="1 3"
Missing quotes will split it to two strings, "too many arguments" error for the [ ]
And compare it with
[[ $number == 1 ]]
where an unquoted $number is always seen as one string, i.e. $number is like "$number".
" is not part of the variable, $number does not contain it. "$number" means whatever is in the variable number should be kept in one. Otherwise " is "only" used to visualize the expression
Code:
[ "" = 1 ]
means you compare the empty string to 1
Code:
[ = 1 ]
means there is nothing on the left side, you compare nothing to 1, which is just syntactically incorrect.
This is all called quotation, and you can find a lot about it here: https://www.gnu.org/software/bash/ma...e/Quoting.html
The " " enforce one string, and $ expressions are evaluated+substituted before the string is handed to the [ ]
Compare with ' ' that enforce one string without substitution.
An empty variable without quotes is nothing, not even an empty string.
Redo the exercise with
number="1 3"
Missing quotes will split it to two strings, "too many arguments" error for the [ ]
And compare it with
[[ $number == 1 ]]
where an unquoted $number is always seen as one string, i.e. $number is like "$number".
I just want to confirm my understanding.
I tried out the following :
Code:
tangara@DESKTOP-SB41UI7:~$ #!/bin/bash
tangara@DESKTOP-SB41UI7:~$
tangara@DESKTOP-SB41UI7:~$ number=
tangara@DESKTOP-SB41UI7:~$
tangara@DESKTOP-SB41UI7:~$ if [ $number = "1" ]; then
> echo "Number equals 1"
> else
> echo "Number does not equal 1"
> fi
-bash: [: =: unary operator expected
Number does not equal 1
tangara@DESKTOP-SB41UI7:~$
is this test trying to tell us not to use "" since "" is for String, thus it is giving Number does not equal 1 ?
Remember that a lot of what "the shell" does is text substitution. It is presented with a text string – having utterly no idea "what it is" – and mechanically performs its substitutions. This is not what "a programming language compiler or interpreter" would do. (The only shell which actually has "a built-in true language interpreter" is Dr. Korn's ksh, which unfortunately is rarely used.)
Last edited by sundialsvcs; 09-13-2023 at 09:00 AM.
In the early minimalistic Bourne shell there were external commands /usr/bin/test and /usr/bin/[
/usr/bin was in the PATH, and
test 1 = 2
and
[ 1 = 2 ]
was like
/usr/bin/test 1 = 2
and
/usr/bin/[ 1 = 2 ]
Later they were replaced by internal (builtin) commands, for better performance.
Still the shell, after doing the usual expansions, invokes the command without knowing what it will do.
The error
-bash: [: =: unary operator expected
is from the bash-internal [ command.
Most Unix and Linux distros still have the external commands for compatibility.
Try the explicit external commands with your border cases! The message text might be different - it's a different implementation.
Later the [[ ]] was introduced; this is directly handled by the bash syntax/parser. It is implemented in a way that it is mostly compatible with a [ ]
But it does less expansions and behaves more like other languages.
And it can do some additional things, that a command cannot do.
Last edited by MadeInGermany; 09-13-2023 at 05:50 PM.
It’s also very worth mentioning the ”#! shebang” feature. If the first line of a “shell script” begins with a line such as #!/usr/bin/perl, the shell will silently transfer control to that script interpreter. (The env command is provided to generalize this.) Therefore, you are not limited to bash’s very limited built-in scripting capabilities. If you look at the source code of most of the “distro-provided shell commands that you use every day,” you will see that nearly all of them use this.
Last edited by sundialsvcs; 09-15-2023 at 10:34 AM.
A friend who was part of the original Santa Cruz Operation and transitioned into the early days of the SCO we are familiar with once passed a small collection of SCO documents to me, including some training materials (which were actually quite good overall).
Among those the #! is consistently referred to as the "hash-pling" - a singularly unmemorable term. I had seen hash-bang, but that was the first and only time I encountered the -pling variant, although wikipedia makes mention of it as an alternate.
Doesn't quite roll off the subvocalized tongue as easily as shebang!
I've never liked the "shebang" term myself: it just seems wrong. I'm ok with the other two, though "hash-bang" sounds more American and "hash-pling" feels more natural to my British ears.
Maybe there's a cultural or age element to whether one considers a '!' as a "pling" or a "bang".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.