0

I'm writing something like the following script. It's doesn't work as expected because the function deleteCEfromCeLst has to call a script specifying to it a "void" parameter through the spawnPrg function, but if I specify in the spawnPrg function parameters the "void" parameter '', that I use when I call the script $HOME/execsh/delete.sh directly from prompt, this "void" parameter results (correctly) as nothing and not as a not specified parameter.

The use of the spawnPrg function is mandatory because it will be used from other functions called from the "main" flow or directly by the main flow, furthermore the function spawnPrg shall contain behaviours act to register something like a log file.

#!/bin/bash

spawnPrg()
{
    echo $@
    $@ &

    return
}

deleteCEfromCeLst()
{
    local d

    cat ce.lst | while read d; do           
        spawnPrg $HOME/execsh/delete.sh $1 '' $d
    done

    return
}

deleteCEfromCeLst
exit

I tried also to code deleteCEfromCeLst as:

deleteCEfromCeLst()
{
    local d

    cat ce.lst | while read d; do           
        spawnPrg "$HOME/execsh/delete.sh $1 '' $d"
    done

    return
}

With the above code the result is that the function spawnPrg is executed as it is sent to the prompt as:

$HOME/execsh/delete.sh $1 \'\' $d

where the "void" parameter is specified as the string '' that is either that the second parameter sent to the delete.sh script is not a "void" parameter.

1 Answers1

4

Quote the variable expansions.

If we have a script foo.sh:

$ cat foo.sh 
#!/bin/sh
args() {
        printf ">%s<\n" "$@"
}
args "$1" "" foo

And we call it with sh foo.sh "", the output is:

><
><
>foo<

That is, one empty argument from "$1" (the argument to foo.sh), another from the hard-coded "" in the script, and then the fixed string foo.

If either of $@ or $1 would have been unquoted, the empty values in question would disappear as part of word splitting.

See:

ilkkachu
  • 138,973