Search code examples
regexbashif-statementposixemoji

Check whether string contains emoji in POSIX if statement


I have written a shell script that outputs a modified time, according to UTC. At present, my if statements to grab and alter the hour are as follows:

if [[ "$emoji" =~ \.☁ ]];
    then
        ((timeShow = time - modValue))
fi

At present, the emoji will be wrapped in format tags for polybar. Namely, the variable will be something like %{F#1a88a4}☁%{F-}. So I am trying to check whether the string contains the ☁ character using the regex above. Unfortunately, this does not appear to work.


Solution

  • In the course of searching for a solution, I found that POSIX Basic Regular Expressions have a different syntax to encapsulate the character and search for it within the string.

    Thus, the correct code should read (changes highlighted), with thanks to @EdMorton:

    if [[ "$emoji" =~ ☁ ]];
        then
            ((timeShow = time - modValue))
    fi