-1

Apologies if what I'm asking is stupid. That said, can someone help me out?

Pop!_OS based on Ubuntu 21.10 | Linux 5.15.8-76051508-generic | wayland display manager

karthik@cosmic:~$ read -p "enter" VAR
enterANything
karthik@cosmic:~$ echo $VAR
ANything
karthik@cosmic:~$ if [ "$VAR"=="Hello" ]
> then
> echo "True"
> fi
True
karthik@cosmic:~$ 
Roman Riabenko
  • 2,145
  • 3
  • 15
  • 39
karthik nair
  • 203
  • 3
  • 12

1 Answers1

0

Because you miss the 'spaces' between "$VAR" and "Hello".

In fact,

if [ "$VAR" == "Hello" ]; then echo "True"; fi

works as should

This is the reason why the statement is broken but I am not a Bash expert, I cannot tell you why Bash behaves in this way.

If someone knows, please share it in the comment!

mattia.b89
  • 3,142
  • 2
  • 14
  • 39
  • Thanks for the quick support.
    adding spaces make it work!
    Regarding why bash works that way, I'll google it.
    Thanks man.
    – karthik nair Jan 02 '22 at 09:57
  • 1
    `[ ... ]`, given a single string between `[` and `]`, returns true if that string is non-empty. It's the same as `[ -n string ]`, and `"$VAR"=="Hello"` is a single non-empty string. – Kusalananda Jan 02 '22 at 11:34