2015-12-19 00:58:41 +0100, Piotr Grzybowski: > Thanks for taking time to answer. Of course it is the issue of > programming style (isnt it true that most things can be brought down > to the programming style?), two real-life examples (stripped-down of > everything): > > #1 > if [ -f ${output}/${branch}/${index}/${current} ]; then > let current--; > for((i=0;i<${branch};i++)) do > index=`new_index ${current} ${index}`; > done; > echo "$^ already filled, regenerating index ${index}/${current}"; > find ${backup}/`basename $^` -type f -exec cp `dirname "$^"/* [..]` > rebuilt ${output} -previous "$^" -branch ${branch} ${index}/${current}; > fi; > > lets assume that for some wicked reason you do not need another local > variable l=${output}/${branch}/${index}/${current}, besides you would > need to remember that it consists of many parts, assemble and > reassemble it, remembering some previous values, also for some reasons > you cant refactor the above code (which of course always can remove > the need for any special variables): lets say that it reflects some > logic. Without $^ it begins to look terrible.
Using a variable is the most obvious thing to do. Also, what if "new_index" was a function which does call "[ -f" on another file? You've got a lot variables and command substitutions that need quoted here btw. > #2 > touch /tmp/`date +%s`; if [ -f /tmp/`date +%s` ]; then echo "ok: $^"; fi; > > just a quick example (the real one was much more complicated in terms > of `` and the latter use of $^) of auto-generated code whos author > really did not want to use another variable, as it would be a waste of > space in his generating code. Without $^ the above requires more care. > As for the all-purpose perl-alike $_ of course I have some ideas, > just let me think, and lets just hope I will not retreat into myself > after you say that $^ is useless ;-) [...] Again, use a variable. Not using a variable is even wrong here, as those two calls of "date" could return a different result. -- Stephane