man bash
gives this about single quoting
Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.
Whatever you type on the command line, bash interprets it and then it sends the result to the program it is supposed to be sent to.In this case, if you use sed 's/$old_run/$new_run/'
, bash first sees the sed
, it recognises it as an executable present in $PATH
variable. The sed
executable requires an input. Bash looks for the input and finds 's/$old_run/$new_run/'
. Single quotes say bash not to interpret the content in them and pass them as they are. So, bash then passes them to sed. Sed gives an error because $
can occur only at the end of line.
Instead if we use double quotes, i.e., "s/$old_run/$new_run/"
, then bash sees this and interprets $old_run
as a variable name and makes a substitution (this phase is called variable expansion). This is indeed what we required.
But, you have to be careful using double quotes because, they are interpreted first by bash and then given to sed. So, some symbols like ` must be escaped before using them.