2

This question is more oriented on what is the best practice to treat arguments in bash functions. Let's take a look to the following code:

#!/bin/bash

do_something () {
        echo "$1"
}

do_something_1 () {
        echo "$1"
}

do_something_2 () {
        echo "$1"
}


do_something_3 () {
        echo "$1"
}

echo "$1"
do_something
do_something "hi"
do_something_2 "hello"
do_something_3 "bye"

And let's imagine I am calling the script:

./myscript.sh param1

This will output:

param1 #First parameter passed to the string
       #Nothing, as I am passing nothing to do_something
hi     #first parameter of do_something
hello  #first parameter of do_something_2
bye    #first parameter of do_something_3

but if I take a look at the functions, all of those are called "$1". Now, I understand this, but this doesn't seems readable. What if the code is bigger? I will need to go to the caller of the function to see what argument was passed (and ignore the parameter that was passed to the script), and it will become more and more difficult to know/maintain what is inside the parameters passed.

  • 2
    If the code gets bigger, rewrite it in a real programming language. Perl, Ruby, or Python come to mind. – choroba Mar 11 '19 at 22:22
  • 1
    Rewriting large programs in another language than bash might be good advise, but it doesn't solve OP's problem: *"I will need to go to the caller of the function to see what argument was passed"*. This is the same for all programming languages I ever programmed in - that's just the concept of a function. – Socowi Mar 11 '19 at 22:34
  • @Socowi Parameter names and types make this way easier in many other languages – that other guy Mar 11 '19 at 22:36
  • This is what comments (in lieu of named parameters) are for: `# $1 - host to connect to; # $2 - port to connect to`, etc. – chepner Mar 11 '19 at 22:38
  • Asking for best practice is too broad and opinion-based, and as such off topic. – l0b0 Mar 11 '19 at 23:01

2 Answers2

4

For larger functions I do:

function myfunc() # source dest [options]
{
    local source="$1"
    local dest="$2"
    local options="$3"
    # Now i have named local variables
    ....
}
Wiimm
  • 2,971
  • 1
  • 15
  • 25
  • If there are especially many parameters or optionals, you can also use `getopts` and have `myfunc -s source -d dest ...` with validation like you do for external utilities – that other guy Mar 11 '19 at 22:34
  • getopt() helps only, if you have options, but not if you have positional parameters (that asked the OP for). – Wiimm Mar 11 '19 at 22:38
  • Flags are positional parameters. There's nothing special about them, there's just a loose convention that most people already know for how a program can tell which positional parameter means what. OP can choose to follow that convention if the (really good) convention you suggest is no longer enough – that other guy Mar 11 '19 at 22:45
-1

You can try :

do_something "$1"

Or you can use a langauage programmation like php if you'll have a big cide

Dev it
  • 21
  • 3