Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append to variable with "read" command

Tags:

variables

bash

With Bash, you can append to a variable, example

$ foo=Hello

$ foo+=world

$ echo $foo
Helloworld

However, is this possible with the read command? Something like

$ foo=Hello

$ read --append foo
world

$ echo $foo
Helloworld
like image 615
Zombo Avatar asked Aug 31 '12 23:08

Zombo


2 Answers

Not directly, so use a temp variable.

foo="Hello"
read tmp
foo+="$tmp"
like image 78
nos Avatar answered Sep 27 '22 22:09

nos


You can fake it, kind of, using readline:

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

When using readline via the -e flag, the argument to -i is put on the first line of the input to get you started. You're not so much appending to foo as giving foo a whole new value, which just happens to begin with the old value if you don't edit the initial line.

like image 41
chepner Avatar answered Sep 28 '22 00:09

chepner