I find that when writing text as input to another program, any command substitutions in double quotes within the intended text are interpreted and expanded by the shell
The links in the answer here states that single quotes can be used to prevent parameter expansion or command substitution. However I'm finding that enclosing a command substitution in single-quotes also fails to stop the shell from expanding the command substitution
How do you prevent the shell from interpreting command substitutions that are intended as text rather than a command to be executed?
A demonstration
$ echo "`wc -l *`"
attempts to count lines in all files in the current directory
$ echo "'`wc -l *`'"
Same result, i.e. counts lines in all files in the current directory
update From this demonstration I've spotted that the problem seems to be that I am quoting the single quotes. I think enclosing single quotes and `
(backtick) in double quotes preserves the literal meaning of (i.e. suppresses) the single quotes but does not preserve the literal meaning of the backquote (i.e. backtick) that introduces the command substitution.
In my use case the input for another command needs to be quoted. With this document saying that:
A single-quote cannot occur within single quotes
How do you prevent a single-quoted command substitution from being expanded when the single-quoted command substitution is within a (double) quoted string? There should be a way to do it other than using backslash escapes
Actual situation
In a program I'm using the only way to split a description of a task into separate lines is to enclose the description in double-quotes:
$ task add "first line doesn\'t say much
Second line says a lot but part of this line does not appear in the resulting description 'truncate -s0 !(temp_file | temp_dir)' truncates all files to 0 bytes as shown by: '`wc -l *`'"
The resulting description:
first line doesn\ -s0 !(temp_file | temp_dir)' truncates all files to 0 bytes as shown by: 0 file1 10 file2 0 directory1 0 directory2 502 file3 123 file4 162 file5 0 directory3
As you can see
't say much
Second line says a lot but part of this line does not appear in the resulting description 'truncate
is missing from the description and the shell has interpreted 'wc -l *'
as a command substitution, thereby including the line counts of all files in the current directory as part of the description
What's causing the shell to remove the part of the argument to task
between \
(backslash) and -s
, and how do you prevent the shell from interpreting the above single-quoted command substitution (i.e. '`wc -l *`'
)?
echo '$(ls)'
does not produce$(ls)
as its output? Can you add the example that is not working for you in the question? – NickD Jul 29 '19 at 20:20$ echo ''`wc -l *`''
. It gives the same result, i.e. tries counting lines in all files in the current directory – bit Jul 29 '19 at 21:00echo '\
wc`'` – Andy Dalton Jul 29 '19 at 21:05echo
demonstration is an example. In my use case the quoted command substitution is in a quoted argument to an external command. The argument to the external command needs to be quoted in other for that particular command to work. Imagine thatecho '`wc -l *`'
is actually within a quoted argument to another command, i.e. something like$ anotherCommand "echo '`wc -l *`'"
– bit Jul 29 '19 at 21:12bar=1; quux=2; echo "foo $bar "'\
wc -l *`'" $quux etc"`. – Jul 30 '19 at 08:47eval
inanotherCommand
? Or passing the command over SSH? Or just expanding a variable containing the command? Things like that matter. – ilkkachu Jul 30 '19 at 10:19echo
example without using backslashes then the solution would easily apply to the actual situation. To answer your question, I'm not executingeval
inanotherCommand
and although I've not looked at the internals ofanotherCommand
I don't think it's executingeval
either – bit Jul 30 '19 at 18:45task add
? An script, a command ? Could you provide or link to its internal description ? – Jul 31 '19 at 17:57task
the only thing I can guess is that there is some double shell expansion which is affected by two different levels of quoting. – Jul 31 '19 at 17:59