Dennis Ebner
Dennis Ebner

Reputation: 55

String starts again after every variable [Bash]

I'm trying to use multiple variables in a string but after each variable the string starts again and overrides the beginning:

#!/bin/bash
var1="ABCDEFG"
var2="hi"
echo "${var1} ${var2}"
echo "$var1 $var2"

It should output

ABCDEFG hi

But both echos output

 hiDEFG

Also if I only use one variable and put text after the variable it still overrides...

There is also an example here: https://stackoverflow.com/a/17862845/8363344

bla=hello
laber=kthx
echo "${bla}ohai${laber}bye"

This should output:

helloohaikthxbye

But it outputs:

byeikthx

I'm starting the .sh with

sudo bash path/bash.sh

But with sudo sh it does not work as well...

I use Ubuntu 16.04 (as a virtual machine)

Thanks Dennis

Upvotes: 1

Views: 61

Answers (1)

SLePort
SLePort

Reputation: 15461

It might be a carriage return character in your input string(s).

You can pipe your echo to a tr command to remove it from output string:

 echo "${var1} ${var2}" | tr -d '\r'

Upvotes: 2

Related Questions