3 ms·
On top of shell glob expansion nuances, I think it should be important to note that I hope nobody runs a destructive action before doing a "dry run" first (unle
by necovek 5y ago
On top of shell glob expansion nuances, I think it should be important to note that I hope nobody runs a destructive action before doing a "dry run" first (unless they trust their backups and/or git history).
IOW, I hope all your find deletes are preceded with a run without the -delete flag. If you get into the habit of doing that and then doing any destructive action using a separate process (piping into xargs), you'll be spared from any hair pulling.
My preference is for any complicated shell magic to first do a run which performs `echo` of whatever operation I was doing on a file (eg. `find | xargs echo sed -i 's/foo/bar/' $file`), but there are other ways to achieve the same thing.
- raffraffraff 5y agoThe echo nukes your quotes so you can't run the precise output of the echo, but yeah, if I'm doing weird shit with variables in a command I do that. Another tip for anyone who runs history commands like `!!` or `rm !$` is the :p operator, which just prints what the shell would have executed and adds the full command into your history so you can hit ^ enter.
- necovek 5y agoIn case it wasn't clear: I'd do the echo run first to validate roughly what commands I would be running (minus some escaping gotchas that you raise), and then another run without the echo. The echo trick is mostly a suggestion of a very quick trick, but it fails in a large number of cases (even something as simple as piping that command through another). Thanks for the pointer to :p too!
- chenxiaolong 5y agoIt's a little bit longer, but bash/zsh support `printf '%q ' <args...>`, which produces output where any characters that may be interpreted by the shell will be escaped.