在POSIX sh中,字符串替换未定义。尝试执行PASS2 =“” $ {PASS // [$ {special_chars}] /}“

问题描述 投票:0回答:1

我的代码是

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

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

我希望PASS2具有PASS中的所有字符-特殊字符。这可以正常工作,但是对此有外壳检查错误。

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

我尝试做

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

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

这些在功能上不起作用。

posix shellcheck
1个回答
0
投票

此脚本通过了shellcheck:

#!/bin/sh

special='!@#$%&'
PASS="e@0cc3auZeeSio&G"
PASS2=$(printf %s "$PASS" | tr -d "$special")
echo "$PASS2"
© www.soinside.com 2019 - 2024. All rights reserved.