To properly concatenate variable reference ${key}
with literal "string"
, embed the variable reference inside the double-quoted string:
key="some"
... | grep "${key}string" | ...
Note how enclosing the variable name in {...}
is actually required now, to tell the shell where the variable name ends. chepner points out in a comment elsewhere:
"${key}
is the canonical way to perform parameter expansion. The braces can optionally be dropped when they aren't necessary to enclose an expansion operator or to disambiguate the parameter name. "
With the specific sample value "some"
, your concatenation (${key}"string"
) would have worked the same, but not generally, because using ${key}
unquoted makes it subject to shell expansions, notably word splitting, which will break the grep
command if the value of variable ${key}
happens to contain whitespace.
The main problem with your code, however, is that you're passing a multi-character string to cut -d
in order to specify a delimiter, whereas only a single character is supported.
If you have GNU grep
, you can try the following:
key="some"
value=$(grep -Po "${key}string"'\K.*$' "${filename}")
This will return everything after "${key}string"
from matching lines, which is what I presume your intent was.
The assumption is that "${key}string"
matches at most once per line.
The small caveat is that you now may have to escape regular-expression metacharacters such as .
in the value of ${key}
and also the literal part ("string"
) to make it work as a regular expression.
With the same assumption and caveat, you can try this POSIX-compliant awk
solution:
key="some"
value=$(awk -F "${key}string" 'NF >= 2 { print $2 }')
That said, if there could be multiple matches, and you needed the strings between the matches on each line, it is possible to adapt this solution.