Add values to a variable in BASH (+=)

Home > Search
  by

Let's say you have a variable in bash that already contains values.

foo="Hello World"

 

The += operator can be used to add values to the variable. In this example, the text "bar" is appended to the variable.

foo+="bar"

 

Printing the variable will now print the following.

Hello World bar

 


Add new lines

In this example, bar followed by a new line is appended to the foo variable.

foo+="bar"
foo+=$'\n'

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments