0

So I was trying to learn Shell Script if else statement. copied this code from internet and it seems... not work properly????????? not really sure what went wrong from here

#!/bin/bash
clear

echo "Enter password"
read pass
if [ $pass="password" ]
then
  echo "The password is correct."
else
  echo "The password is incorrect, try again."
fi

Expected output should be : Password is incorrect

Instead I get : Password is correct

enter image description here

markp-fuso
  • 28,790
  • 4
  • 16
  • 36
Linkachus 17
  • 33
  • 1
  • 5
  • 1
    Replace `[ $pass="password" ]` with `[ "$pass" = password ]` – M. Nejat Aydin Nov 19 '22 at 18:22
  • 2
    if this is *exactly* what the *`code from the internet`* looks like then it's wrong from a syntax perspective; when troubleshooting code consider cutting-n-pasting your code (along with shebang) into [shellcheck.net](https://www.shellcheck.net/) and make the recommended changes; in this case shellcheck is going to tell you about the need to have a space on each side of the `=` – markp-fuso Nov 19 '22 at 18:26
  • BTW, this is a problem that http://shellcheck.net/ will identify for you automatically; the error it throws is [SC2077](https://www.shellcheck.net/wiki/SC2077). – Charles Duffy Nov 19 '22 at 18:37
  • 1
    It seems you've copied the script from [this article](https://www.digitalocean.com/community/tutorials/if-else-in-shell-scripts). The article contains inaccuracies. Don't follow it. – M. Nejat Aydin Nov 19 '22 at 18:54
  • 1
    @M.NejatAydin Oof. That's... embarrassing. – chepner Nov 20 '22 at 02:14
  • @M.NejatAydin thank you, didn't know the tutorial is incorrect – Linkachus 17 Nov 20 '22 at 08:36

0 Answers0