Skip to main content
2 of 4
added 17 characters in body
030
  • 1.6k
  • 2
  • 17
  • 33

Which regular expression methods to validate input could be used in shell scripting?

#!/bin/sh

re="\/$"

if [ $1 =~ $re ]; then
        echo "${ATTENTION_PREFIX}$1 DIRECTORY MAY NOT CONTAIN A \"/\" OR LITERAL SLASH!${ATTENTION_POSTFIX}"
        exit 1
fi

Executing sudo file.sh hello/ results in [: 29: hello: unexpected operator

It looks like that this regular expression method is incorrect for shell scripting.

030
  • 1.6k
  • 2
  • 17
  • 33