Add to variable with read command

With Bash you can add to a variable like

$ foo=Hello $ foo+=world $ echo $foo Helloworld 

However, is this possible with the read command? Sort of

 $ foo=Hello $ read --append foo world $ echo $foo Helloworld 
+4
source share
2 answers

You can fake it, for example, using readline :

 $ foo=Hello $ read -e -i"$foo" foo Hello 

When using readline with the -e flag, the -i argument is placed on the first line of input to get you started. You are not so much adding foo to foo as you are assigning foo whole new value that just starts with the old value if you are not editing the start line.

+3
source

Not directly, so use a temporary variable.

 foo="Hello" read tmp foo+="$tmp" 
+6
source

All Articles