Expansion of variable inside single quotes in a command in Bash

bash variable inside double quotes
bash escape single quote
bash escape quotes in variable
bash escape double quotes in variable
bash escape single quote in variable
bash add double quotes to variable
bash variable in string
sed variable inside single quotes

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.

Inside single quotes everything is preserved literally, without exception.

That means you have to close the quotes, insert something, and then re-enter again.


Word concatenation is simply done by juxtaposition. As you can verify, each of the above lines is a single word to the shell. Quotes (single or double quotes, depending on the situation) don't isolate words. They are only 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

script="echo \"Argument 1 is: $myvar\""
/bin/sh -c "$script"

If the contents of $myvar is untrusted, here is an exploit:

myvar='foo"; echo "you were hacked'

Instead of the above invocation, use positional arguments. The following invocation is better -- it's not exploitable:

script='echo "arg 1 is: $1"'
/bin/sh -c "$script" -- "$myvar"

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.

Is there any way to print value inside variable inside single quote , You can't expand variables in single quotes. Or, you can backslash double quotes inside of double quotes: eval echo 'visit:$site' visit:unix.stackexchange.​com Full demonstration: cd /var && QUOTED='cd $HOME' && echo $QUOTED && $(eval echo "$QUOTED") -- first command moves you to a  Its a bash script running in bash shell and repo forall command takes parameters inside single quotes. If I substitute the actual value, the command runs fine. – Rachit Dec 10 '12 at 11:49

The repo command can't care what kind of quotes it gets. If you need parameter expansion, use double quotes. If that means you wind up having to backslash a lot of stuff, use single quotes for most of it, and then break out of them and go into doubles for the part where you need the expansion to happen.

repo forall -c 'literal stuff goes here; '"stuff with $parameters here"' more literal stuff'

Explanation follows, if you're interested.

When you run a command from the shell, what that command receives as arguments is an array of null-terminated strings. Those strings may contain absolutely any non-null character.

But when the shell is building that array of strings from a command line, it interprets some characters specially; this is designed to make commands easier (indeed, possible) to type. For instance, spaces normally indicate the boundary between strings in the array; for that reason, the individual arguments are sometimes called "words". But an argument may nonetheless have spaces in it; you just need some way to tell the shell that's what you want.

You can use a backslash in front of any character (including space, or another backslash) to tell the shell to treat that character literally. But while you can do something like this:

echo \"Thank\ you.\ \ That\'ll\ be\ \$4.96,\ please,\"\ said\ the\ cashier

...it can get tiresome. So the shell offers an alternative: quotation marks. These come in two main varieties.

Double-quotation marks are called "grouping quotes". They prevent wildcards and aliases from being expanded, but mostly they're for including spaces in a word. Other things like parameter and command expansion (the sorts of thing signaled by a $) still happen. And of course if you want a literal double-quote inside double-quotes, you have to backslash it:

echo "\"Thank you. That'll be \$4.96, please,\" said the cashier"

Single-quotation marks are more draconian. Everything between them is taken completely literally, including backslashes. There is absolutely no way to get a literal single quote inside single quotes.

Fortunately, quotation marks in the shell are not word delimiters; by themselves, they don't terminate a word. You can go in and out of quotes, including between different types of quotes, within the same word to get the desired result:

echo '"Thank you. That'\''ll be $4.96, please," said the cashier'

So that's easier - a lot fewer backslashes, although the close-single-quote, backslashed-literal-single-quote, open-single-quote sequence takes some getting used to.

Modern shells have added another quoting style not specified by the POSIX standard, in which the leading single quotation mark is prefixed with a dollar sign. Strings so quoted follow similar conventions to string literals in the ANSI C programming language, and are therefore sometimes called "ANSI strings" and the $'...' pair "ANSI quotes". Within such strings, the above advice about backslashes being taken literally no longer applies. Instead, they become special again - not only can you include a literal single quotation mark or backslash by prepending a backslash to it, but the shell also expands the ANSI C character escapes (like \n for a newline, \t for tab, and \xHH for the character with hexadecimal code HH). Otherwise, however, they behave as single-quoted strings: no parameter or command substitution takes place:

echo $'"Thank you.  That\'ll be $4.96, please," said the cashier'

The important thing to note is that the single string received as the argument to the echo command is exactly the same in all of these examples. After the shell is done parsing a command line, there is no way for the command being run to tell what was quoted how. Even if it wanted to.

command line - variable expansion in single quote, Your double quotes are technically still inside single quotes so no expansion can happen. You can close the single quotes before starting the  the exclamation point is not single-quoted. Because history expansion occurs so early in the parsing process, the single quotes are just part of the double-quoted string; the parser hasn't recognized the command substitution yet to establish a new context where the single quotes would be quoting operators.

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.

Bash escape quotes – Linux Hint, These are escape characters, single quotes and double quotes which are explained with `echo` command prints the value of this variable without any quotation. Sometimes it is required to print a single quote inside a string. Bash Shell Expansions: Brace Expansion, Parameter Expansion and more · Making The Bash  A single quote may not occur between single quotes, even when preceded by a backslash. Double Quotes Enclosing characters in double quotes ( " ) preserves the literal value of all characters within the quotes, with the exception of $ , ` , \ , and, when history expansion is enabled, ! .

just use printf

instead of

repo forall -c '....$variable'

use printf to replace the variable token with the expanded variable.

For example:

template='.... %s'

repo forall -c $(printf "${template}" "${variable}")

Expansion of variable inside Single Quotes Fails!!, I am unable to expand the value of entry variable inside the nawk command. I tried three different nawk command as below but none of them substitute the value  Shell Variable Expansion The difference between single and double quotes becomes more important when you’re dealing with variables on the command line—by default, bash is going to expand a variable like $test into the full string. First, we’ll assign the variable: test="This is a test"

Below is what worked for me -

hive -e "alter table TBL_NAME set location $QUOTE$TBL_HDFS_DIR_PATH$QUOTE"

[TIP] Force variable substitution within single quotes in BASH, Sometimes in a BASH script you need to have single quotes but the value inside them is actually a variable. Here is how to have both the single  Bash escape character is defined by non-quoted backslash (\). It preserves the literal value of the character followed by this symbol. Normally, $ symbol is used in bash to represent any defined variable. But if you use escape in front of $ symbol then the meaning of $ will be ignored and it will print the variable name instead of the value

Quoting - Linux Shell Scripting Tutorial, For example, $var is used to expand the variable value. You can enable or disable the meaning of a special character by enclosing them in single quotes. 3.5.3 Shell Parameter Expansion. The ‘$’ character introduces parameter expansion, command substitution, or arithmetic expansion.The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.

Quoting (The GNU Awk User's Guide), It is impossible to embed a single quote inside single-quoted text. Refer back to The shell does at least variable and command substitution on the quoted text.

UNIX Shell Quotes - a simple tutorial, Using the echo command; Quoting a single character with the backslash; Strong Inside the single quotes, you can include almost all meta-characters: You can enable variable expansion to POSIX and Bourne shell scripts