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?
echo '<add-command-here>'
? Where does that data (the commands to run) come from? – ilkkachu Mar 22 '21 at 22:11tcsh
the shell you are working in? – Kusalananda Mar 22 '21 at 22:20"echo " + escapeString(cmd)
to the script file. And My question tries to figure out whatescapeString
should do. – vesii Mar 23 '21 at 12:14"$cmd"
. I haven't used a *csh shell for (sighs) over 20 years. – glenn jackman Mar 23 '21 at 13:40$PWD
it will print the pwd). 2) What if the cmd already have double quotes? – vesii Mar 23 '21 at 14:02cat << "EOT"
your_command
"EOT"
).