Adding more text to a variable | Unix Linux Forums | Shell Programming and Scripting

  Go Back    

Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Adding more text to a variable

Shell Programming and Scripting

Closed Thread    
Thread Tools Search this Thread Display Modes
Old 01-04-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 216 Times in 204 Posts
Adding more text to a variable

Is there a more simple way to add text to a variable
I have

and like to add green
to get
echo $var
blue green

Here is how I do it.

var=$(echo "${var} green")

---------- Post updated at 13:23 ---------- Previous update was at 13:12 ----------


Not always easy to see the simple...
This works fine

var="${var} green"

Sponsored Links
Old 01-04-2013
RudiC RudiC is offline Forum Advisor  
Registered User
Join Date: Jul 2012
Last Activity: 23 November 2014, 8:49 AM EST
Location: Aachen, Germany
Posts: 4,685
Thanks: 78
Thanked 1,158 Times in 1,088 Posts
in bash:
$ var+=" green"
$ echo $var
blue green

The Following User Says Thank You to RudiC For This Useful Post:
Jotne (01-04-2013)
Sponsored Links
Old 01-04-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Join Date: Jul 2012
Last Activity: 22 November 2014, 4:49 PM EST
Location: San Jose, CA, USA
Posts: 5,082
Thanks: 195
Thanked 1,701 Times in 1,444 Posts
Note that the braces aren't needed either unless what you want to add to the variable could be interpreted by the shell to be part of the variable name.

echo "$var"
var="$var green"
echo "$var"
echo "$var"


blue green
blue greenred

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[bash help]Adding multiple lines of text into a specific spot into a text file cdn_humbucker Shell Programming and Scripting 2 03-06-2010 03:11 AM
adding value to variable using expr Kochu77 Shell Programming and Scripting 2 11-22-2008 08:39 AM
Adding specific text and spaces to each line in a text file hertingm Shell Programming and Scripting 4 08-25-2008 03:34 PM
adding spaces for a variable value dnat Shell Programming and Scripting 2 02-26-2008 05:32 AM
adding 0 to a variable shivakundan Shell Programming and Scripting 3 02-14-2007 10:05 PM

All times are GMT -4. The time now is 10:28 AM.