On Mon, Aug 23, 2010 at 16:40, Eric Blake <ebl...@redhat.com> wrote: > On 08/23/2010 03:29 PM, Peng Yu wrote: >> >> Hi, >> >> I'm wondering if there is a widely accepted coding style of bash scripts. >> >> lug.fh-swf.de/vim/vim-bash/StyleGuideShell.en.pdf >> >> I've seen the following style. Which is one is more widely accepted? >> >> if [ -f $file]; then >> do something >> fi >> >> if [ -f $file]; >> then >> do something >> fi > > Neither. You're missing adequate quoting in case $file contains spaces. > More importantly, when using '[', the trailing ']' has to be its own > argument. Personally, I tend to use the style with fewer lines: > > if [ -f "$file" ]; then > do something > fi
This is also possible: [ -f "$file" ] && do_something or perhaps: [ -f "$file" ] && { do_something_0 do_something_1 }