Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and 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 ----------

Linux 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 offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 27 July 2015, 4:19 PM EDT
Location: Aachen, Germany
Posts: 6,377
Thanks: 112
Thanked 1,759 Times in 1,645 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 online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 27 July 2015, 9:27 PM EDT
Location: San Jose, CA, USA
Posts: 6,792
Thanks: 289
Thanked 2,270 Times in 1,941 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

Linux 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 09:27 PM.