csh and variable values with spaces

my working shell is csh and even though if I try to run my script in plain sh, it behaves the same way. Here's a simple script:

#!/bin/sh
desc='"test my changes"'
cmd="echo \"$desc\""
$cmd

I want $desc to be passed as an argument to another command, but csh apparently doesn't like spaces in the string and interprets the value as this:

This is the real problem as the subsequent command (in reality it's another script instead of an echo doesn't understand the value of $desc when it sees

Any solutions how to properly escape spaces in a string variable?

What's your ultimate goal? I'm not quite understanding your need to jam everything into a string with extra quotation marks instead of just passing the strings you want in the first place... Instead of

cmd="command \"$stuff\""

couldn't you just run

command "$stuff"

in the first place?

Or, what exactly are you trying to pass, into what? Don't just show me code that doesn't work, I can't guess what you actually do want from it.

my goal is to call another script within my script which will look like

COMMENT="To test my script"
CMD="./anotherscript.sh -c \"$COMMENT\""
$CMD

I need my $COMMENT variable to expand properly as a whole stirng.

That still doesn't explain the purpose behind this odd goal, which is what I was wondering, because there may be more straightforward ways to do it than the design you've chosen.

In sh, you can try eval "$cmd" to force it to re-evaluate the line again after the first expansion, which will indeed interpret the literal quotes as a string parameter.

This is dangerous however, because eval accepts all valid shell syntax, including backticks and variables. Someone could feed it a tailored string to extract variables from your program or run whatever commands they wanted.

Because of this you may wish to make the string into

CMD="./anotherscript.sh -c \"\$COMMENT\""

to prevent $COMMENT from being expanded. eval will expand it for you, but won't interpret its contents as anything other than string. I don't think this is possible in C shell, only sh.

Sorry for not being clear. I'm trying to create a wrapper script that will call either script1 or script2 depending on what option users select on command line when calling my script. One more input that my script takes and later passes to script2 is the comment line which can be a string with spaces. I'm then trying to pass that string as an argument to script2. The string has to be enclosed within double quotes for script2 to accept it properly.

I still don't see any need to put it all in one string like that, then. Just keep them separate, and run them like normal. You can do the double-quoting yourself instead of torturing the syntax.

$!/bin/sh
case "$1" in
a)
        exec ./myscripta -c "$2"
        ;;
b)
        exec ./myscriptb -c "$2"
        ;;
*)
        echo "Unknown command '$1'" >&2
        exit 1
        ;;
esac