I have a string that is the result of some operation I have no control over. When I print this variable using echo, I get:echo $myvarhelloHowever, when I doif [ $myvar = hello ]; then    echo they are equalelse    echo they are not equalfiI always get that they are not equal. I suspect this is because of a newline character. The string also behaves strangely. When I do:newVAR=this is my var twice: $myvar$myvarecho $newVARI get:hellois my var twice: helloHow can I check if this is in fact due to a newline and, if so, remove it?
How to assert that a string has a newline character and, if so, remove it
bash;string
The problem is that you have an embedded Carriage-Return (CR, \r). This causes the terminal text-insertion point to kick back to the start of the line it is printing. That is why you are seeing the 'hello' at the start of the line in you $newVAR example - sed -n l displays a readable view of unprintable characters (and end of line).  var=ab$'\r'c ; echo $var;  printf %s $var | sed -n l# output:cbab\rc$You can test for it with a simple bash condition check:  [[ $var == *$'\r'* ]] && echo yes || echo no# output:yesYou can combine the test and fix in one step by testing for \r(s) and removing them via:    fix=${var//$'\r'/}; echo $var; echo $fix# output:cbabcThe fix uses Shell Parameter Expansion. The particular form used above is for replacing substrings based on your proovided pattern: ${parameter/pattern/string} <-- This replaces only the first found pattern with string in variable named *parameter. To replace all patterns, you just need to change the first / to //. 