How to change the value of a function's parameter in linux script?

bash, linux, shell

Solution

You could make use of the `set` builtin in order to change a positional parameter.

The following snippet changes the first positional parameter, i.e. `$1`, to `something`:

set -- "something" "${@:2}"

As an example, refer to the following:

echo "Original parameters: $@"
set -- "something" "${@:2}"
echo "Modified parameters: $@"

Assuming this was placed in a script called `script`, and was invoked by saying `bash script foo bar baz`, it'd output:

Original parameters: foo bar baz
Modified parameters: something bar baz

Quoting from `help set`:

`set: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]`

 Set or unset values of shell options and positional parameters.

Problem

I have a script which I call with different parameters. Depending on the value of those parameters I check out and build the 'parameter' SVN version of the project. ``` ./deploy 3281 ``` This command will create a 3281 directory and check out the 3281 SVN version of the project and will build it in the 3281 directory. I need to create a key word 'HEAD' so the script will check to see the latest SVN revision number and create a folder for it (ex: 3282 ) and then checkout the head version of the project and build it there. I find out how to get the latest revision number with svn ( svn info -r 'HEAD' --username jse http://jse@svn.ctsvpn.com/repos/Teleena/ | grep Revision | egrep -o "[0-9]+" ) and I am trying to simply implement an if like this: ``` #check to see if latest/head revision is called if [ "$1" == "head" ]; then #get latest revision number HEADREV=$(svn info -r 'HEAD' --username jse http://jse@svn.ctsvpn.com/repos/Teleena/ | grep Revision | egrep -o "[0-9]+") echo "==========================================" echo "= Revision number: $HEADREV will be used =" echo "==========================================" #change swap the second parameter $1=$HEADREV #<-- IS THIS CORRECT? fi ...[rest of program here] ``` I want to replace the first parameter with the latest revision number and leave the rest of the script untouched. So the question is: How to I change a function's parameter value from inside the function?

Original source