Bash Append Text To a Variable

Bash Append Text To a Variable

To append text to a variable in Bash, you can use the += operator. This operator allows you to add text to the end of a variable, without overwriting its existing value.

Here's an example of how to use the += operator to append text to a variable:

# Declare a variable and assign it a value
message="Hello"

# Append text to the variable
message+=" World"

# Print the variable
echo "$message"
S‮cruo‬e:www.lautturi.com

The output of this script will be "Hello World".

You can also use the += operator to append the value of another variable to the first variable. For example:

# Declare two variables
greeting="Hello"
name="John"

# Append the value of the second variable to the first
greeting+=" $name"

# Print the first variable
echo "$greeting"

The output of this script will be "Hello John".

Note that the += operator only works with variables that have been declared using the declare or local commands, or with variables that have been exported using the export command. It does not work with variables that are created simply by assigning them a value.

Created Time:2017-10-27 14:56:34  Author:lautturi