---
title: Named Parameters In Bash
date: 2018-10-17
tags: bash, parameters, shell script
source: https://brianchildress.co/blog/named-parameters-in-bash
---

# Named Parameters In Bash

Sometimes we need to pass multiple arguments to a shell script and often we don't need to pass all arguments to our script every time, that's where _named parameters_ comes in. With named parameters we can pass something like "--school hard knocks" to our script. However in bash this isn't something we have available to us by default, but there is a cool little function that can help.

The idea is to pass your _named parameter_ as an argument starting with two dashes (--) followed by the name of the parameter a space and the parameter value. The function looks like this:

_my-script.sh_
```sh
#!/bin/bash

environment=${environment:-production}
school=${school:-is out}

while [ $# -gt 0 ]; do

   if [[ $1 == *"--"* ]]; then
        param="${1/--/}"
        declare $param="$2"
        # echo $1 $2 // Optional to see the parameter:value result
   fi

  shift
done

echo $environment $school
```
_(credit to the StackOverflow poster for the start of this solution)_

To call this script passing parameters:
```console
./my-script.sh --environment dev
>> dev is out
```

OR

```console
./my-script.sh --random-parameter random-value
>> production is out
```

The script is assigning both _school_ and _environment_ to the default values that are assigned at the beginning of the script. We also have a new parameter cleverly named _random-parameter_ that has a value of _random-value_.

It's pretty simple but let's go through each section of the script to understand what's happening.

At the beginning we assign a couple variables some default values, this way if the user doesn't pass the values our script will at least continue to run successfully. By assigning the variable name back to itself we're saying that the script should take any parameter that is passed if not use the default. The default value is identified with the '[parameter name]:-[default value].

```sh
environment=${environment:-production}
school=${school:-is out}
```

Next we have a simple while loop that first evaluates if we have any passed parameters, if so it will proceed with looping.

```sh
while [ $# -gt 0 ]; do
...
done
```

On each iteration of the while loop we call the _[shift](https://ss64.com/bash/shift.html)_ command. _Shift_ is a built-in bash function that renames a parameter based on the position of the arguments. Essentially we are shifting the pointer for an argument to the next argument after each loop. By doing this we can work our way through the list of arguments, no matter the length, and assign the argument to their value.

Inside of our while loop we loop over each of the parameters looking for the "--" indicator, once we find one we assign the string directly after the "--" to the parameter name and the string directly following to it's value.

```sh
...
if [[ $1 == *"--"* ]]; then
    v="${1/--/}"
    declare $v="$2"
    # echo $1 $2 // Optional to see the parameter:value result
fi
...
``` 

This is a simple way to pass parameters to a shell script, indicating the name and value with a simple identifier.
