2 added 238 characters in body
source | link

You should quote variable expansions (just one example from your code):

sed $finalSedPattern

is unquoted and will be split on spaces (when used). You should use:

sed "$finalSedPattern"

Your function with quotes:

function getVal {  
    sedPattern='s/^.*"keyVal":"\([^"]*\)".*$/\1/1'
    finalSedPattern="${sedPattern/keyVal/$2}"
    echo "$(sed "$finalSedPattern"  <<< "$1")"
}

And with optional spaces:

function getVal {
    sedPattern='s/^.*"keyVal"[ ]\{0,\}:[ ]\{0,\}"\([^"]*\)".*$/\1/1'
    finalSedPattern="${sedPattern/keyVal/$2}"
    echo "$(sed "$finalSedPattern"  <<< "$1")"
}

You should quote variable expansions (just one example from your code):

sed $finalSedPattern

is unquoted and will be split on spaces (when used). You should use:

sed "$finalSedPattern"

Your function with quotes:

function getVal {  
    sedPattern='s/^.*"keyVal":"\([^"]*\)".*$/\1/1'
    finalSedPattern="${sedPattern/keyVal/$2}"
    echo "$(sed "$finalSedPattern"  <<< "$1")"
}

You should quote variable expansions (just one example from your code):

sed $finalSedPattern

is unquoted and will be split on spaces (when used). You should use:

sed "$finalSedPattern"

Your function with quotes:

function getVal {  
    sedPattern='s/^.*"keyVal":"\([^"]*\)".*$/\1/1'
    finalSedPattern="${sedPattern/keyVal/$2}"
    echo "$(sed "$finalSedPattern"  <<< "$1")"
}

And with optional spaces:

function getVal {
    sedPattern='s/^.*"keyVal"[ ]\{0,\}:[ ]\{0,\}"\([^"]*\)".*$/\1/1'
    finalSedPattern="${sedPattern/keyVal/$2}"
    echo "$(sed "$finalSedPattern"  <<< "$1")"
}
1
source | link

You should quote variable expansions (just one example from your code):

sed $finalSedPattern

is unquoted and will be split on spaces (when used). You should use:

sed "$finalSedPattern"

Your function with quotes:

function getVal {  
    sedPattern='s/^.*"keyVal":"\([^"]*\)".*$/\1/1'
    finalSedPattern="${sedPattern/keyVal/$2}"
    echo "$(sed "$finalSedPattern"  <<< "$1")"
}