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
    #1  
Old 01-04-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 8 April 2014, 11:00 AM EDT
Posts: 1,036
Thanks: 61
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
Code:
var="blue"

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

Here is how I do it.

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

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

Uff

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

Code:
var="${var} green"

Sponsored Links
    #2  
Old 01-04-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 20 April 2014, 9:39 AM EDT
Location: Aachen, Germany
Posts: 3,525
Thanks: 51
Thanked 820 Times in 783 Posts
in bash:
Code:
$ var+=" green"
$ echo $var
blue green

The Following User Says Thank You to RudiC For This Useful Post:
Jotne (01-04-2013)
Sponsored Links
    #3  
Old 01-04-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 20 April 2014, 2:06 PM EDT
Location: San Jose, CA, USA
Posts: 3,471
Thanks: 141
Thanked 1,202 Times in 1,018 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.

Code:
var=blue
echo "$var"
var="$var green"
echo "$var"
var="${var}red"
echo "$var"

produces

Code:
blue
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 02:11 AM
adding value to variable using expr Kochu77 Shell Programming and Scripting 2 11-22-2008 07:39 AM
Adding specific text and spaces to each line in a text file hertingm Shell Programming and Scripting 4 08-25-2008 02:34 PM
adding spaces for a variable value dnat Shell Programming and Scripting 2 02-26-2008 04:32 AM
adding 0 to a variable shivakundan Shell Programming and Scripting 3 02-14-2007 09:05 PM



All times are GMT -4. The time now is 02:59 PM.