I need to substitute a portion of an awk command in a script with different cases, from this:CONTAINER=$1RESULTS=$(ssh -o LogLevel=QUIET -t -i ~/key.pem user@server sudo docker stats --no-stream $CONTAINER | awk '{if (NR!=1) {print substr($2, 1, length($2)-1)}}')to this:#!/bin/bashCONTAINER=$1TYPE=$2case ${TYPE} in       cpu)        AWK='{if (NR!=1) {print substr($2, 1, length($2)-1)}}'    ;;   esacRESULTS=$(ssh -o LogLevel=QUIET -t -i ~/key.pem user@server sudo docker stats --no-stream $CONTAINER | awk $AWK)But I always get a syntax error.
Substitute command as variable into bash script
bash;awk
In a variable declaration, anything (other than parameter/command/arithmetic expansion) inside  is treated literally, so when you do:var='foobar'$var will be expanded to 'foobar', not foobar.So you need:AWK='{if (NR!=1) {print substr($2, 1, length($2)-1)}}'as you don't need any pre-expansion by shell, better to use single quotes to stay on the safer side. If you use double quotes, then e.g. $2 will be expanded beforehand by the shell as parameter expansion, this is true for any other tokens that are special to shell.And don't forget to quote $AWK when you use it to disable the split+glob operator: | awk $AWK