Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash, Concatenating 2 strings to reference a 3rd variable

Tags:

I have a bash script I am having some issues with concatenating 2 variables to call a 3rd.

Here is a simplification of the script, but the syntax is eluding me after reading the docs.

server_list_all="server1 server2 server3";
var1 = "server";
var2 = "all";

echo $(($var1_list_$var2));

This is about as close as I get to the right answer, it acknowledges the string and tosses an error on tokenization.

syntax error in expression (error token is "server1 server2 server3....

Not really seeing anything in the docs for this, but it should be doable.

EDIT: Cleaned up a bit

like image 422
Im Fine Avatar asked Jun 12 '12 04:06

Im Fine


People also ask

How do I concatenate string variables in bash?

The += Operator in Bash Bash is a widely used shell in Linux, and it supports the '+=' operator to concatenate two variables. As the example above shows, in Bash, we can easily use the += operator to concatenate string variables.

How do you concatenate strings and variables?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

How do you concatenate in shell?

String concatenation is the process of appending a string to the end of another string. This can be done with shell scripting using two methods: using the += operator, or simply writing strings one after the other.

How do I combine multiple strings into one?

You can concatenate a list of strings into a single string with the string method, join() . Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.


2 Answers

The Bash Reference Manual explains how you can use a neat feature of parameter expansion to do some indirection. In your case, you're interested in finding the contents of a variable whose name is defined by two other variables:

server_list_all="server1 server2 server3"
var1=server
var2=all
combined=${var1}_list_${var2}

echo ${!combined}

The exclamation mark when referring to combined means "use the variable whose name is defined by the contents of combined"

like image 168
Eric Smith Avatar answered Oct 07 '22 18:10

Eric Smith


The Advanced Bash Scripting Guide has the answer for you (http://tldp.org/LDP/abs/html/ivr.html). You have two options, the first is classic shell:

 #!/bin/bash

 server_list_all="server1 server2 server3";
 var1="server";
 var2="all";

 server_var="${var1}_list_${var2}"
 eval servers=\$$server_var;

 echo $servers

Alternatively you can use the bash shortcut ${!var}

 #!/bin/bash

 server_list_all="server1 server2 server3";
 var1="server";
 var2="all";

 server_var="${var1}_list_${var2}"
 echo ${!server_var}

Either approach works.

like image 25
Recurse Avatar answered Oct 07 '22 19:10

Recurse