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

1k Views Asked by At

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.

1

There are 1 best solutions below

3
On BEST ANSWER

This script passes shellcheck:

#!/bin/sh

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