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...
🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.
🐧 2 comments so far... add one ↓
🐧 2 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
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
http://tldp.org/LDP/abs/html/bashver3.html – for more info check it out
The += operator is now permitted in in places where previously only the = assignment operator was recognized.
a=1
echo $a # 1
a+=5 # Won’t work under versions of Bash earlier than 3.1.
echo $a # 15
a+=Hello
echo $a # 15Hello