Bash script to show conditional if-else statement.



  • Take into account the main readme for this section. README first.

    Code.

    $ cat learnbash.sh
    #!/bin/bash
    
    Message="Hello There!"
    read -p "What is your name? " Name
    
    if [ "$Name" == "Josh" ]
    then
      echo "$Message $Name, you can go home early today."
    else
      echo "$Message $Name, you have to stay late for production work."
    fi
    

    Run the script.

    $ ./learnbash.sh 
    What is your name? Josh
    Hello There! Josh, you can go home early today
    

    What happens if you're not Josh?

    $ ./learnbash.sh 
    What is your name? Jill   
    Hello There! Jill, you have to stay late for production work.
    


© Lightnetics 2024