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

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-04-2013
Jotne's Unix or Linux Image
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
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 ----------

Unix or Linux Image Uff

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

Code:
var="${var} green"

Sponsored Links
    #2  
Old Unix and Linux 01-04-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 4 March 2015, 3:33 AM EST
Location: Aachen, Germany
Posts: 5,419
Thanks: 94
Thanked 1,411 Times in 1,329 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 Unix and Linux 01-04-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 4 March 2015, 2:58 AM EST
Location: San Jose, CA, USA
Posts: 5,766
Thanks: 235
Thanked 1,913 Times in 1,630 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 04:33 AM.