Differences between revisions 4 and 10 (spanning 6 versions)
Revision 4 as of 2007-11-07 07:28:17
Size: 1486
Editor: c-68-56-54-232
Comment:
Revision 10 as of 2018-07-06 10:01:46
Size: 1604
Editor: 83
Comment: Added howto concatenate arrays
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq13)]] <<Anchor(faq13)>>
Line 3: Line 3:
There is no (explicit) concatenation operator for strings (either literal or variable dereferences) in the shell; you just write them adjacent to each other:
Line 4: Line 5:

There is no (explicit) concatenation operator for strings (either literal or variable dereferences) in the shell, *BUT* one can concat the values of variables by sequentially naming eaching variable to be joined together. The variables whose values are to be concatenated are written one after the other:

{{{
    var=$var1$var2
{{{#!highlight bash
var=$var1$var2
Line 13: Line 11:
{{{
    var="$var1 - $var2"
{{{#!highlight bash
var="$var1 - $var2"
Line 19: Line 17:
{{{
    var=$var1/.-
{{{#!highlight bash
var=$var1/.-
Line 25: Line 23:
{{{
    var=${var1}xyzzy
    # Without braces, var1xyzzy would be interpreted as a variable name
{{{#!highlight bash
var=${var1}xyzzy
# Without braces, var1xyzzy would be interpreted as a variable name
Line 29: Line 27:
    var="$var1"xyzzy
    # Alternative syntax
var="$var1"xyzzy
# Alternative syntax
Line 35: Line 33:
{{{
    logname="log.$(date +%Y-%m-%d)"
{{{#!highlight bash
logname="log.$(date +%Y-%m-%d)"
Line 39: Line 37:
There's no difference when the variable name is reused, either: There's no difference when the variable name is reused, either. A variable's value (the string it holds) may be reassigned at will:
Line 41: Line 39:
{{{
    string="$string more data here"
{{{#!highlight bash
string="$string more data here"
}}}

Concatenating arrays is also possible:

{{{#!highlight bash
var=( "${arr1[@]}" "${arr2[@]}" )
Line 47: Line 51:
{{{
    string+=" more data here" # EXTREMELY non-portable!
{{{#!highlight bash
string+=" more data here" # EXTREMELY non-portable!
Line 52: Line 56:

----
CategoryShell

How can I concatenate two variables? How do I append a string to a variable?

There is no (explicit) concatenation operator for strings (either literal or variable dereferences) in the shell; you just write them adjacent to each other:

   1 var=$var1$var2

If the right-hand side contains whitespace characters, it needs to be quoted:

   1 var="$var1 - $var2"

If you're appending a string that doesn't "look like" part of a variable name, you just smoosh it all together:

   1 var=$var1/.-

Otherwise, braces or quotes may be used to disambiguate the right-hand side:

   1 var=${var1}xyzzy
   2 # Without braces, var1xyzzy would be interpreted as a variable name
   3 
   4 var="$var1"xyzzy
   5 # Alternative syntax

CommandSubstitution can be used as well. The following line creates a log file name logname containing the current date, resulting in names like e.g. log.2004-07-26:

   1 logname="log.$(date +%Y-%m-%d)"

There's no difference when the variable name is reused, either. A variable's value (the string it holds) may be reassigned at will:

   1 string="$string more data here"

Concatenating arrays is also possible:

   1 var=( "${arr1[@]}" "${arr2[@]}" )

Bash 3.1 has a new += operator that you may see from time to time:

   1 string+=" more data here"     # EXTREMELY non-portable!

It's generally best to use the portable syntax.


CategoryShell

BashFAQ/013 (last edited 2018-07-06 10:01:46 by 83)