I want to run a command from a bash shell script which has single quotes and some other commands inside the single quotes and a variable.
e.g. repo forall -c '....$variable'
In this format, $
is escaped and the variable is not expanded.
I tried the following variations but they were rejected:
repo forall -c '...."$variable" '
repo forall -c " '....$variable' "
" repo forall -c '....$variable' "
repo forall -c "'" ....$variable "'"
If I substitute the value in place of the variable the command is executed just fine.
Please tell me where am I going wrong.
just use printf
instead of
use printf to replace the variable token with the expanded variable.
For example:
Does this work for you?
Below is what worked for me -
Variables can contain single quotes.
Inside single quotes everything is preserved literally, without exception.
That means you have to close the quotes, insert something, and then re-enter again.
As you can verify, each of the above lines is a single word to the shell. String concatenation is simply done by juxtaposition. Quotes (single or double quotes, depending on the situation) are used to disable interpretation of various special characters, like whitespace,
$
,;
... For a good tutorial on quoting see Mark Reed's answer. Also relevant: Which characters need to be escaped in bash?Do not concatenate strings interpreted by a shell
You should absolutely avoid building shell commands by concatenating variables. This is a bad idea similar to concatenation of SQL fragments (SQL injection!).
Usually it is possible to have placeholders in the command, and to supply the command together with variables so that the callee can receive them from the invocation arguments list.
For example, the following is very unsafe. DON'T DO THIS
If the contents of
$myvar
is untrusted, here is an exploit:Instead of the above invocation, use positional arguments. The following invocation is better -- it's not exploitable:
Note the use of single ticks in the assignment to
script
, which means that it's taken literally, without variable expansion or any other form of interpretation.EDIT: (As per the comments in question:)
I've been looking into this since then. I was lucky enough that I had repo laying around. Still it's not clear to me whether you need to enclose your commands between single quotes by force. I looked into the repo syntax and I don't think you need to. You could used double quotes around your command, and then use whatever single and double quotes you need inside provided you escape double ones.