How to send variable to an inline shell-script?

Either use export to turn it into an environment variable, or pass it directly to the command.

VAR="Test" sh -c 'echo "Hello $VAR"'

VAR="Test"
export VAR
sh -c 'echo "Hello $VAR"'

Avoid using double quotes around the shell code to allow interpolation as that introduces command injection vulnerabilities like in:

sh -c " echo 'Hello $VAR' "

causing a reboot if called when $VAR contains something like ';reboot #


Here's yet another way to pass variables to sh -c (as positional arguments):

{
VAR="world"
VAR2='!'
sh -c 'echo "Hello ${0}${1}"' "$VAR" "$VAR2"
}

If you don't want to export them as environment variables, here's a trick you could do. Save your variabe definition to a file .var_init.sh and source it in your sub-shell like this:

.var_init.sh

VAR="Test"

from the command line:

sh -c ". .var_init.sh && echo \$VAR" # Make sure to properly escape the '$'

This way, you only set your variables at the execution of your subshell.