-4

‍‍This is my code:

Number=12
ListOperation=('+'  '/'  '*'  '-')
if [[ " ${ListOperation[*]} " == *"/"* ]]; then
    let Result="$Number $operation $Number"
    echo $Result
fi

Why is "[]" used twice in "if? Why "*" is used next to the "/" string?

  • 4
    That's a number of questions there. See e.g. [What is the difference between the Bash operators [[ vs vs ( vs ((?, http://mywiki.wooledge.org/BashGuide/TestsAndConditionals, https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html, http://mywiki.wooledge.org/BashGuide/Arrays and https://www.gnu.org/software/bash/manual/html_node/Arrays.html – ilkkachu May 03 '22 at 11:55
  • 6
    This is quite basic bash syntax. Have you read any documentation? Is this your first contact with bash scripting? If so, please try reading a tutorial or the bash documentation first, that will make it much easier for you to learn. That said, this code is wrong and doesn't do what it should be doing so we can't really answer. I mean, the main answer for you is help [[, but nothing in the code you show makes sense so we cannot really tell you "why". – terdon May 03 '22 at 12:01
  • 1
    @terdon guessing it's homework to write a two expression one operator infix calculator – Chris Davies May 03 '22 at 13:38
  • @roaima Yes I am trying to design a simple calculator – ahmadreza1383 May 03 '22 at 17:18
  • @terdon Yes, Sure I will read the documents – ahmadreza1383 May 03 '22 at 17:19
  • 1
    I'd suggest you spend some time working through a beginner's tutorial for bash – Chris Davies May 03 '22 at 17:19

1 Answers1

0

You can think of the [[...]] as a control character sequence that signals to bash to evaluate whatever expression is found in between them as a conditional expression.

Likewise, the * is also a special control character, which tells bash to "match anything". As you get more adept with programming, you will learn that it's proper name is a meta character but that's not important for now.

To learn about bash conditional expressions, you can type man bash on your terminal then skip to the section by entering /, and on the resulting prompt, typeing CONDITIONAL EXPRESSIONS followed by Enter. You may need to type n a few times to get to the actual section.

Have fun as you learn.

AdminBee
  • 22,803