0

I am still snooping around bash as a newbie and I wanted to ask some question Is it possible to append additional text to value in bash? Example below:

#!/bin/bash
value1="qwerty"
value2="asdfgh"

echo $value1 >> $value3
echo $value2 >> $value3
echo $value3

So my question is - can I create value that contain other values? Why am I trying to do this? Because I want to do some logical functions for each value and if it meets the criteria it gets appended to my final value. By the end I would echo value3 and it would contain all results that met criteria.

veetay
  • 13
  • 2
  • 2
    `value3=${value1}; value3+=${value2}` – William Pursell Mar 23 '22 at 13:15
  • 1
    `$value3` is, specifically, the *value* of the variable, not the variable itself. If `>>` *did* work with variables, you would write something like `echo "$value1" >> value3` (But in reality, this just appends the contents of `value1` to a file named `value3`.) – chepner Mar 23 '22 at 13:16

1 Answers1

0

>> is for writing to files, not appending to variables. You just want string interpolation.

value3="$value1$value2"

If you want an embedded newline, you can do that:

value3="$value1
$value2"

If you want to append a value to an existing variable,

value3=$value1

value3="$value3
$value2"
chepner
  • 497,756
  • 71
  • 530
  • 681