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 "s/a/b/g"
.
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 sed
command).
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 -f
option:
sed -f noquotes.txt <<<"aaa"
Also, the 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 y/a/b/
.
If this is the only operation that you need to perform, then it may be even more efficient to use the simpler tr
utility:
tr a b <<<"aaa"
spaces.txt
with content-e "s/a /b/g"
. Now when I tryecho "a a" | sed $(cat spaces.txt)
I get a similar error. Wouldspaces.txt
be better written as-e s/a /b/g
? This seems contrary to the command-line usage of sed where I need to add quotes around the sed command like this:echo "a a" | sed -e "s/a /b/g"
– mark May 03 '16 at 21:11echo "a b " | sed -e "s/a /b/g" -e "s/b /c/g"
=>bc
. What should the contents ofmulti.txt
be if I want to writeecho "a b " | sed "$(cat multi.txt)"
and similarly produce outputbc
? – mark May 03 '16 at 21:34;
insed
. You're right, this is an XY question. In reality I want to pass multiple-x module
arguments to thebrowserify
command, where each module is specified in a newline-separated file, and eachmodule
is contained in double-quotes within the file. Themodules
may be file paths – mark May 03 '16 at 22:22sed
as an example hoping it would be familiar to the unix.stackexchange community – mark May 04 '16 at 00:23browserify
thingie... It looks like you're trying to build up a command line based on the contents of some files so Etan's suggestion there isn't bad though I'm not sure why you have posted the question there - stackoverflow is a black hole - questions are forgotten in the next 3 minutes. Wait a couple of days and if you don't get any reply delete it from there and re-post it here on Unix&Linux (don't cross-post as it will be closed). – don_crissti May 04 '16 at 10:55