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

    #declaration block

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


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

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

I have currently referred to the following websites.




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


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).

  • 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 – Dodzi Dzakuma Oct 1 '13 at 5:07

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


    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))
     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
    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 5 '13 at 20:43

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"

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.