3

I have been programming in BASH for a while now and the scripts that I write are starting to get more complicated.

I realized that I started to bring in some habits from C/C++ and wanted to get confirmation on if I needed to break my current habits or not. The reason being, not all programming languages are the same and they should be used to capitalize on their strengths.

Using Variables

function()
{
    #declaration block
    runCommand=

    for string in "$@"; do
        runCommand="$runCommand $string"
    done
}

vs

function()
{
    for string in "$@"; do
        runCommand="$runCommand $string"
    done
}

Which is advised for BASH/SH and for what reasons?

I have currently referred to the following websites.

http://lug.fh-swf.de/vim/vim-bash/StyleGuideShell.en.pdf

https://stackoverflow.com/questions/15610794/bash-coding-conventions

http://wiki.bash-hackers.org/scripting/style

Edit: My question was a little unclear so I changed the code example a little bit.

3 Answers3

3

If you don't want that inherited environment variables are used as initial values, it is important to initialize the variables you are using (or unset them explicitly). An environment variable named "runCommand" will not influence your first snipset, it will be used in your second one. (That may be a feature, but you have to be aware of it).

And you need to be aware that this is true also for variables which are interpreted by the shell (IFS for instance).

AProgrammer
  • 10,404
  • 1
  • 30
  • 45
  • I found a linke that confirms your answer. Thanks a lot. I'll make sure to always initialize my variables before using them unless I need to do otherwise. [link](http://www.dedoimedo.com/computers/bash-tricks.html) – Dodzi Dzakuma Oct 01 '13 at 05:07
2

Some things I like to do to keep my scripts maintainable:

  • use functions

    Instead of putting everything into one giant script file create several small functions

    Example:

    main() {
      # put logic here
    }
    
    main "$@" # pass commandline args to main function
    
  • within functions, declare variables with the local modifier

    main() {
      local result
    
      (($1 > 10) && result="greater than" || result="less than"
    }
    
    echo "$result" # result is not visible outside main
    
  • variables in bash are not block scoped but lexically scoped so putting them into 'blocks' doesn't buy you much (bash actually has no blocks)

     for i in {1..10}; do
       result=((result + i))
     done
    
     echo "$result" # still visible here
    
  • still I think declaring variables at the point where they are needed is best

To come back to your example the loop is actually unnecessary. If you want to execute a command with "$@" as parameters just do

"$runCommand" "$@"
Oliver Weiler
  • 2,448
  • 19
  • 28
  • 2
    While `local` is implemented by several shells, it's not part of POSIX and won't be guaranteed portable to systems that implement that standard. – Blrfl Sep 27 '13 at 10:19
  • @Blrfl You have made a really good point. And I should always consider how portable I **need** my scripts to be. Also, taking what AProgrammer stated about variable usage issues, initializing before use is something that I need to keep in mind. The feedback has been great and it has already helped me clean up some of my code. **=>** How important is POSIX to my life as a programmer on UNIX systems? – Dodzi Dzakuma Sep 27 '13 at 15:48
  • I didn't know that I could just use `"$@"` as it was! Thanks for the tip. There is actually some code that I omitted because it was specific to my work, but now that I know that I can just plug in `$@` I can clean up a lot of my work! When I first started BASH programming, I was using `local variable=` a lot, but recently I kind of stopped. There actually is no reason for this. As long as I'm programming strictly for BASH I should use `local variable=` to save me headaches. Especially when I will be exporting functions and variables all over the places and using script includes. Right? – Dodzi Dzakuma Sep 27 '13 at 15:53
  • 1
    @DodziDzakuma: Using `local` is fine as long as you make sure it's clear that your scripts use them and prevent them from running in a POSIX-pure Bourne shell. The safest way to do that is make sure your shebang line reads `#!/bin/bash` and not `#!/bin/sh`. – Blrfl Oct 05 '13 at 20:43
2

A few tips.

  • Local variables are declare local and initializated to null if strings or 0 if number
  • if you develop in bash use bash. (+=, for arg;do done;...)
  • Use always builtin [[ notnumber ]] or (( number )) never [,test,( or any other

    function() {
    local runCommand=
    local -i index=0
      #you don't need the extra parameters here
      for strings; do
       runCommand+=" $strings"
      done 
    }
    

The vast majority part of bash you can find online, even on your distro, is very basic/improper bash if you really want to learn it go to Greg's wiki. It's really difficult to find that kind of advises/knowledge for Bash.

more2000
  • 31
  • 3