posixshellcheck

In POSIX sh, string replacement is undefined. while trying to do PASS2="${PASS//[${special_chars}]/}"


My Code is

special_chars='[=!=][=@=][=#=][=$=][=%=][=&=]'
PASS="e@0cc3auZeeSio&G"

PASS2="${PASS//[${special_chars}]/}"

I want PASS2 to have all the characters in PASS - special characters. This works fine, but there is shell check error on this.

PASS2="${PASS//[${special_chars}]/}"
             ^-- SC2039: In POSIX sh, string replacement is undefined.

I tried doing

PASS2=$(printf '%s' "$PASS2" | PASS//["${special_chars}"]/)

And

PASS2=$(printf '%s' "$PASS" | PASS//["${special_chars}"]/)

These does not work functionally.


Solution

  • This script passes shellcheck:

    #!/bin/sh
    
    special='!@#$%&'
    PASS="e@0cc3auZeeSio&G"
    PASS2=$(printf %s "$PASS" | tr -d "$special")
    echo "$PASS2"