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,532
  • 1
  • 32
  • 48
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" "$@"
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.