Bash String Concatenation

by on November 18, 2010 · 2 comments· LAST UPDATED November 18, 2010

in , ,

How do I join two strings under BASH? I have two strings stored in two different variables (_p=/delta and _u=aqua) and how do I join this and assign it to another variable in bash?

You can use parameter substitution as follows:

### join two $vars ###
echo "${_p}${_u}"
echo "${_p} and ${_u}"
echo "${_p}/${_u}"
echo "Output: $out"

Another example:

echo "Host: ${HOSTNAME}@$(date) by $USER"
echo "Setting Apache for ${_domain} @ ${_path}...."
# setupJail "${_domain}" "${_path}"

Sample outouts:

Host: Nov 19 01:44:03 IST 2010 by root
Setting Apache for @ /chroot/
Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 2 comments… read them below or add one }

1 Anthony Thyssen November 22, 2010 at 11:35 pm

Assigned the variables in a quoted string, you can include or not include spaces, commas or anything else as you like.

a=”part one”
b=”part two”
c=”$a, $b”
echo “$c”
part one, part two


2 droopy4u February 13, 2011 at 2:47 pm – for more info check it out

The += operator is now permitted in in places where previously only the = assignment operator was recognized.

echo $a # 1

a+=5 # Won’t work under versions of Bash earlier than 3.1.
echo $a # 15

echo $a # 15Hello


Leave a Comment

Tagged as: , , , , , , , , , ,

Previous Faq:

Next Faq: