User Tools

Site Tools


bash:strings:concatenating_strings

BASH - Strings - Concatenating Strings

To concatenate two variables you can just write them one after another:

#!/bin/bash
 
string1="Hello"
string2="World"
string=$string1$string2
 
echo "$string is the concatenation of $string1 and $string2."

NOTE: The following program outputs the string “HelloWorld is the concatenation of Hello and World.”.


a='Hello'
b='World'
c="${a} ${b}"
echo "${c}"
> Hello World

or

foo="Hello"
foo="${foo} World"
echo "${foo}"
> Hello World

var="abcdefgh"
echo ${var:0:(-1 -4)}XYZ

returns:

abcXYZ
bash/strings/concatenating_strings.txt · Last modified: 2021/01/13 22:55 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki