LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-11-2023, 12:48 AM   #1
tangara
LQ Newbie
 
Registered: Sep 2023
Posts: 2

Rep: Reputation: 0
why it becomes if [ "" = "1" ]; then when the problem is fixed ?


i refer to the tutorial on staying out of trouble

http://linuxcommand.org/lc3_wss0090.php

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 ?
 
Old 09-11-2023, 01:04 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Welcome to LQ!

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
 
2 members found this post helpful.
Old 09-11-2023, 06:08 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,807

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
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".
 
Old 09-11-2023, 11:27 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,363

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
As per post 3, prefer [[ ]] to [ ] : https://tldp.org/LDP/abs/html/testco...ml#DBLBRACKETS.

As an aside, '==' is string comparison, ' -eq ' is numeric comparison; easy to get wrong, esp when comparing '0' (zero).


See also
https://tldp.org/LDP/abs/html/compar...l#ICOMPARISON1
 
1 members found this post helpful.
Old 09-12-2023, 02:20 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,954

Rep: Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329
" 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
 
Old 09-13-2023, 08:46 AM   #6
tangara
LQ Newbie
 
Registered: Sep 2023
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MadeInGermany View Post
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 ?
 
Old 09-13-2023, 08:54 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,954

Rep: Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329Reputation: 7329
no
#!/bin/bash
should only be used at the beginning of scripts, otherwise it is a comment line, does nothing

number=
will declare a variable named number and will assign the empty string to it

[ $number = "1" ]
here $number will be replaced with the value stored in that variable, the result will be:

[ = 1 ]
which is just syntactically incorrect, = is not an unary operator, cannot be used directly after [

[ "$number" = "1" ]
will be translated to:

[ "" = "1" ]
so the shell will compare the empty string to 1 (as string, not number)
 
Old 09-13-2023, 08:54 AM   #8
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,146
Blog Entries: 6

Rep: Reputation: 1833Reputation: 1833Reputation: 1833Reputation: 1833Reputation: 1833Reputation: 1833Reputation: 1833Reputation: 1833Reputation: 1833Reputation: 1833Reputation: 1833
Quote:
-bash: [: =: unary operator expected
number is false
Code:
n=
if [ "$n" = "1" ]; then
    echo "Number equals 1"
else
    echo "Number does not equal 1"
fi
 
Old 09-13-2023, 08:58 AM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,676
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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.
 
Old 09-13-2023, 05:45 PM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,807

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
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.
 
Old 09-15-2023, 10:31 AM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,676
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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.
 
Old 09-15-2023, 12:52 PM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,807

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
The kernel (not the shell) will run the #! script interpreter!
The shell is one of the possible interpreters.
If you want to read more:
https://stackoverflow.com/questions/...e-shebang-work
 
1 members found this post helpful.
Old 09-15-2023, 01:38 PM   #13
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
OT Trivia: Shebang!

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!
 
1 members found this post helpful.
Old 09-15-2023, 06:34 PM   #14
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,910

Rep: Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026
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".
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
suddenly vlc audio chunky. got sound then no sound then got sound then no sound.. andrewysk Linux - Newbie 4 04-21-2021 08:19 PM
LXer: Microsoft thinks it's fixed Windows Server mess its last fix 'fixed' LXer Syndicated Linux News 0 06-06-2016 05:30 AM
LXer: Tor becomes extra secure as .onion becomes Special-Use Domain Name LXer Syndicated Linux News 0 09-14-2015 07:00 PM
Bug in 8.04, fixed in 8.10 - How to get fixed in 8.04 which is LTS? taylorkh Ubuntu 4 02-28-2009 05:17 PM
When backspace becomes ^[[D and up arrow becomes ^[[A simonb Linux - Software 1 12-21-2008 01:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration