I'm creating a tcsh script on the fly (with a static language). I have a <add-command-here> section that contains some command. I want to do:
echo <add-command-here>
In that <add-command-here> I just insert the command. The command should be printed as-is and no evaluated. For example if it contains env $PWD I want it to print $PWD and not the actual path. I thought of wrapping it with single quotes:
echo '<add-command-here>'
But it won't work if the command already have quotes, since here it says:
A single-quote cannot occur within single-quotes.
I also can't use double quotes because it will still evaluate and also it will have the same problem if the command already contains double quotes.
Some tests:
main.py --help
main.py -option1 $PWD
main.py -option1 '$PWD'
'main.py -option1 $PWD'
main.py -option1 "$PWD"
main.py -option1 "PWD 'hi'"
main.py -option1 "PWD \'hi\'"
I want all of them to be printed as-is, meaning if the input is X the output should be X. I'm guessing we need to escape each one of the special char. I will do the implemented by myself (just consider it as a black box - some function that does it). My question only focus on strategy. What is the right way to solve it here?