How to assert that a string has a newline character and, if so, remove it
17
votes
4
answers
18778
views
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 $myvar
hello
However, when I do
if [ $myvar = "hello" ]; then
echo they are equal
else
echo they are not equal
fi
I 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$myvar
echo $newVAR
I get:
hellois my var twice: hello
How can I check if this is in fact due to a newline
and, if so, remove it?
Asked by farid99
(583 rep)
Jul 9, 2015, 10:54 PM
Last activity: Jul 14, 2024, 02:19 PM
Last activity: Jul 14, 2024, 02:19 PM