3

Found this example on the arch wiki

#!/bin/sh
case $1/$2 in
  pre/*)
    echo "Going to $2..."
    ;;
  post/*)
    echo "Waking up from $2..."
    ;;
esac

and can't make out if it's a logical operator or something else.

8

There is nothing special here. The syntax for case is

case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac

In the example in the question, word is built by combining $1, / and $2. / doesn't have a special meaning at all, it's just a character.

PS: Practically the usage is a bit strange, as the $2 part afterwards gets matched against * so the value of $2 doesn't really matter. One could, in the context described in the Wiki, also write

#!/bin/sh
case $1 in
  pre)
    echo "Going to $2..."
    ;;
  post)
    echo "Waking up from $2..."
    ;;
esac

But there might be cases where suspend, hibernate or hybrid (the possible values for $2) are relevant, so it's just a general pattern here.

  • 3
    maybe $1 is pre/heat and $2 is oven in which case your code would not match, but the original would ;-) – mosvy Dec 22 '18 at 17:19
  • 1
    @mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha. – nohillside Dec 22 '18 at 17:23
  • They would not be equivalent in cases where $1 is pre/something. – Stéphane Chazelas Dec 22 '18 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.