So, Let's say I have the following pattern:
Thisisatest="1"
in a file called file
.
And I want to match the exact string above but for whatever reason, I choose to loop over some numbers from a list, use those as variable and try to do this:
Thisisatest="$varhere"
Where the above $varhere
is equal to the number above, being 1
. (while i mentioned looping over number, this was just a future possible use cases for this and remain an example)
Now to make things easier, say I want to match the above exact pattern while using the above variable and it's content:
grep 'Thisisatest="$varhere"' file
Where file contain the string Thisisatest="1"
and $varhere
contain the number 1
.
Problem being, this wouldn't work because variable expansion doesn't occur when in-between single quotes (like above).
Here the failed attempt at countering this:
- substitution of content from variable substitution
echo "${varhere/[0-9]*/Thisisatest=\"$varhere\"}"
Here I use echo
to see if it output the right string so it can be used as input for grep...it output this instead:
'Thisisatest="'1'"
- Add more quotes
echo "'Thisisatest="${varhere}"'"
output:
'Thisisatest='1''
echo ''''Thisisatest=\"${varhere}\"''''
output:
Thisisatest="'1'"
The rest is obvious... Now the very last attempt above seems to be close to what i want, but still not it.
Any way(s) to do the above?