Monday, April 23, 2012

Bash String Concatenation

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:
 
_p="/delta"
_u="aqua"
### join two $vars ###
out="${_p}${_u}"
echo "${_p}${_u}"
echo "${_p} and ${_u}"
echo "${_p}/${_u}"
echo "Output: $out"
 
Another example:
#!/bin/bash
_domain="${1:-example.com}"
_root="/chroot"
_path="${_root}/${_domain}"
echo "Host: ${HOSTNAME}@$(date) by $USER"
echo
echo "Setting Apache for ${_domain} @ ${_path}...."
# setupJail "${_domain}" "${_path}"
 
Sample outouts:
 ./setup.httpd nixcraft.net.in
Host: www34.nixcraft.com@Fri Nov 19 01:44:03 IST 2010 by root
Setting Apache for nixcraft.net.in @ /chroot/nixcraft.net.in...

No comments:

Post a Comment