LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-09-2018, 03:35 AM   #1
KatrinAlec
Member
 
Registered: Feb 2012
Posts: 116

Rep: Reputation: 13
bash programming question


I'm trying things out with if and spaces
why is
if [ " " = "String" ];
different from
if [ " "="String" ];
?
 
Old 08-09-2018, 04:38 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
There must be spaces around the operator.

I assume you have a mistake in the comparison as you compare two constants. One of the should be a variable.

jlinkels
 
Old 08-09-2018, 04:42 AM   #3
KatrinAlec
Member
 
Registered: Feb 2012
Posts: 116

Original Poster
Rep: Reputation: 13
Of course in real life it would be variables.
Just for the sake of example I used values, same problem if I use variables.
 
Old 08-09-2018, 06:01 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
As stated bash conditional statements have a required syntax. There must be a space between the brackets and between the operand. No spaces will usually produce an error.

Code:
if [ "foo" = "bar" ]
then
   ...
fi

if [ "foo" == "bar" ]
then
   ...
fi

https://www.gnu.org/software/bash/ma...pressions.html

Last edited by michaelk; 08-09-2018 at 06:03 AM.
 
Old 08-09-2018, 06:33 AM   #5
KatrinAlec
Member
 
Registered: Feb 2012
Posts: 116

Original Poster
Rep: Reputation: 13
It seems there are different opinions on what is right on the internet.
I've found:
if [ "foo" = "foo" ] - so with spaces.
if [ "$a" = "$b" ] - with spaces and with quotes, probably if a variable contains a$="Hello World" it needs the quotes.
But I've also found
if [ a$=b$ ] - no space, no quotes (might of course always be a mistake)

So you say the correct method is with spaces, and I guess quotes around variables will also be a good idea.
Is there any reason ever not to use
if [ "a$" = "b$" ]
?
 
Old 08-09-2018, 07:03 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
There is a difference between double and single quotes. Single quotes will preserve the literal value of each character within the quotes. Double quotes will expand variables i.e. $foo to its actual value and \ which is used as an escape character.

Quote:
[ "$a" = "$b" ]
For basic if statements using quotes is a good practice.

Last edited by michaelk; 08-09-2018 at 11:32 AM. Reason: fixed variables.
 
1 members found this post helpful.
Old 08-09-2018, 07:40 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
a$ is probably $a, $ at the end is quite unusual. And probably will not do what you want.
Learning variables, quoting is not trivial in bash, but you can have help to understand it.
At first there is a man page and other documentation too (just look for bash quoting).
Also you can use set -xv in your script to check how it is really working and also you can check your script using shellcheck (or similar)
 
1 members found this post helpful.
  


Reply

Tags
bash, programming, spaces



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
a question about bash programming tianlijian Linux - Newbie 3 03-16-2009 05:44 AM
Bad: help me bash programming , newbie question CheeSen Programming 2 09-02-2008 09:23 PM
Question about bash shell programming walterbyrd Linux - General 1 11-01-2005 09:42 AM
very simple bash programming question!? Thinking Programming 7 06-01-2005 11:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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