Team for a beautiful quote

Sometimes I need to quote the entire command line for future evaluation. I usually do this with:

printf "%q " " $@ " 

This one is short and sweet, but the result looks awful. In most cases this does not matter, but in some cases I want to show it to the user. For example, in the history of the menu of executable commands, which allows you to re-record. In this case, I would like to quote from a more readable form (closer to what the user himself would do if he was responsible for the citation). So:

 search 'Wordreference (eng->spa)' utter 

would be preferable:

 search Wordreference\ \(eng-\>spa\) utter 

To get the first cited form, I was able to iterate " $@ " and do something like the following for each argument:

 [[ $arg == *\ * ]] && arg="'"${arg//\'/\'\\\'\'}"'" 

This is not complicated, but includes a loop, conditional string conversion, and concatenation of the result of each iteration.

I wonder if there is a โ€œwith batteriesโ€ command to make this conversion out of the box.

+6
source share
1 answer

Similarly, you use eval to execute a line later; you can use eval to print it:

 eval "echo $yourstring" 

This will remove the shell screens, but keep your variable intact.

+2
source

All Articles