Posts

Showing posts from December, 2021

Following Google's Shell Style

Image
I use Bash scripts quite often for many internal tools. One of the scripts I use everyday now grows to about 300 lines. Its purpose is to auto fetch various internal data (such as multiple JIRAs) and organize the results into one single page, which saves me a lot of time. I have read the Google Shell Style long ago,  and finally got some time during Xmas break to follow the guidelines. Here are the major findings: Use  $(command)  instead of backticks. Backticks refers to `, for example results=`cmd arg1 arg2` The reasons not to use backticks anymore are due to backticks an be messy and not good for nesting. If you have backticks inside backtick, you would need to escape them. A good example here libdir=$(dirname $(dirname $( which gcc)))/lib libdir=`dirname \`dirname \\\` which gcc\\\`\``/lib $ is more safe and predictable.  Use (( ... )) or $(( ... )) instead of expr (command) . The reasons not to use expr is, it is simply too old. For the difference of (( ... )...