2

I am a bit new to linux shell scripting, so this could be a very silly question.

This is a simple example code (of course it does not work, but I am hoping to show what I wanna do):

#!/bin/bash 
function AppendLetters() {
  # Value sent as parameter: $1
  $1= "$1"LLL
}
var="foo"
AppendLetter $var
echo "$var"

So, when calling the program from command line:

$ ./example.sh

I would like to modify the internal variableobtain some sort of:

fooLLL

Reason for doing this: I have a script that loads multiple variables from a config file, and would like to make the same modification to that variables in order to use them in my program.

Is it possible? Can a function modify variables sent as parameter?

1

2 Answers 2

2

Without needing bash 4.3, using variable indirection:

AppendLetters() {
  declare -g "$1"="${!1}LLL"
}

var=f00
AppendLetters var
echo "$var"
f00LLL

The -g option for declare is necessary so that the assignment is not treated as local to the function.


Given:

I have a script that loads multiple variables from a config file, and would like to make the same modification to that variables in order to use them in my program.

I would do this, not in a function, using bash's += assignment operator

varlist=( var1 var2 var3 )
for varname in "${varlist}"; do
    declare "$varname"+="LLL"
done
Sign up to request clarification or add additional context in comments.

3 Comments

Same comment as in anubhava's post: you could definitely use declare -g "$1+=LLL" (this completely avoids indirection). Or use printf.
Excuse me, @glennjackman, could you please tell me what does the ! symbol means in the ${!1}LLL part?
Follow the "variable indirection" link at the top of my answer.
2

Starting from Bash 4.3.0 you can use declare -n to get reference of a variable in a function:

AppendLetters() {
   declare -ln ref="$1"
   ref+="LLL"
}

var="foo"
AppendLetters "var"

echo "$var"
fooLLL

From help declare:

-n  make NAME a reference to the variable named by its value

EDIT: Thanks to @gniourf_gniourf for suggesting this printf, you can do this in older BASH:

AppendLetters() { printf -v "$1" '%sLLL' "${!1}"; }

var="foo"
AppendLetters "var"

echo "$var"
fooLLL

7 Comments

Rulez! But is there any special reason for your { instead of my " in the appending LLL line?
You could do ref="$ref"LLL also but this gives you flexibility to add a value with space also like: ref="${ref}LLL MM NN"
With Bash<4.3, you'd do: declare "$1"+=LLL. If you want to use declare -n (which is not really justified for this anyway, unless you want to explicitly check that you have a valid identifier—yet you could use printf for that), it's still better to use ref+=LLL. Also, declare your ref variable local as declare -ln ref="$1". Another option is to use printf with the -v switch.
You can use printf as: printf -v "$1" '%sLLL' "${!1}".
It's safer to use the command I gave: printf -v "$1" '%sLLL' "${!1}" in case the expansion of ${!1} yields percents or backslashes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.