Tuesday 20 October 2020

bash -c "$pitfall"

If user use bash -c "" will easy to get unexpected result if user just copy-paste working code from existing shell session into bash -c "" (OR extend existing bash -c "") and expect it same. But if user use single quotes(outermost) consistently, then user can get rid a lot of trouble.

So, always use ''
single quotes in outermost(of course you can insert "$var" inner to expand if really need while you aware of this pitfall).

Previously I thought bash -c "" OR '' is just favor of choice(sort of isolate process something) until one day I play around with find/xargs and I wonder why `bash -c "dd2=`echo 5` && echo $dd2"` print nothing and why some commands pattern only run correctly on second time(because second time that "global" "$variable" get initialized from first time).

Note that I'm not talking about export environment thing here. `bash -c ''` is just one of the fundamental every shell user MUST know or you waste your time when debugging commands, and worst, unnoticed hidden bug wait for trigger.


No comments:

Post a Comment