Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

http://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.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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

share|improve this answer
    
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
add comment

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" "$@"
share|improve this answer
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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