There are a few things wrong with your commands.
string=#@$AAA%*
and a=#$@%*
: here the $AAA
and $@
parts are interpreted as variables. You should single-quote the strings so that they are interpreted literally.
- For the
sed
part, you are asking it to replace a literal string of #$@%*
in order with nothing. If you want separate characters, you'd need to use a different format, e.g. the character class [#$@%*]
. However, in this case, I'd just use tr -d
instead, which is much simpler.
- Also,
$(…)
is preferred over backticks.
- I'd also use a here-string (
<<<
) instead of an echo, just to save a few cycles.
- I noticed that Kusalananda edited my answer to add
"
quotes around variables. I deliberated over this too, but figured I wouldn't because it would be simpler, and not necessary for your specific question. However, I agree that this is the "right" way to do things. I've added "
quotes around the other variables and the $(…)
construct; in this case they are unnecessary, but also good practice.
Hence the final commands are
string='#@$AAA%*'
a='#$@%*'
b="$(<<<"$string" tr -d "$a")"
echo "$b"