Search code examples
linuxbashshellawkpositional-parameter

BASH shell execution from string with positional parameters


When I try to run the code below, the shell is replacing (because they are not defined as a bash variable) $4 and $2 with blanks. My question is, how do I keep bash from trying to evaluate the positional parameters for awk as its variables? I've tried putting double and single quotes around the positional parameters, however, that did not suppress bash from interpreting them as local variables instead of strings.

This is what is returned when I echo "$x$i$y"

date -r /root/capture/capture11.mp4 | awk '{print }' | awk -F":" '/1/ {print }'

Code:

#!/bin/sh
i=$(cat /etc/hour.conf)
x="date -r /root/capture/capture"
y=".mp4 | awk '{print $4}' | awk -F\":\" '/1/ {print $2}'"
$x$i$y

Any help would be greatly appreciated!


Solution

  • $4 is doubled quoted. Though there are single quotes, it is included in double quotes. So the single quotes are just part of the string and it won't keep the literal meaning of $. So you can escape the $:

    y=".mp4 | awk '{print \$4}' | awk -F\":\" '/1/ {print \$2}'"
    

    Or, use single quotes around the whole part:

    y='.mp4 | awk "{print \$4}" | awk -F':' "/1/ {print \$2}"'