There is a step in the processing of the command line that is called the quote removal step. This is usually the last thing that happens before the command is executed and it removes the outer sets of quotes that you used to quote strings in the command.
For a command such as
sed -e "s/a/b/g"
this step make sure that the
sed command is given the string
s/a/b/g as its last argument, and not
When you are reading the string
"s/a/b/g" from a file using
cat in a command substitution, the quotes will not be removed, as they are not part of the original command (they are part of the data read from a file).
This means that
sed will receive the literal string
"s/a/b/g" as the expression to run, and it therefore complains that it does not understand the initial
" (it was expecting a
As for the output from
set -x tracing, treat it as nothing more than debugging output. The
bash shell will add quotes around strings that contain certain characters in the trace, so it's not indicative of what the quoting of any single argument actually was or how it was interpreted by the invoked utility.
As an aside, to run
sed with an editing script on file, use the
sed -f noquotes.txt <<<"aaa"
y command in
sed could be used to transliterate between one set of single characters to another more efficiently than a substitution with
s would. Your
s/a/b/g substitution is better written as
If this is the only operation that you need to perform, then it may be even more efficient to use the simpler
tr a b <<<"aaa"