chomp like Perl operator in Bash

I am sure there should exist a chomp like Perl operator in Bash using which I can literally remove new line characters as show below:

Any clue?

#!/bin/bash
str1="Hello\n"
str2="World"
echo -e "$str1.$str2"
echo -e "\n"

str1="${str1%\\n}"

echo -e "$str1.$str2"
echo -e "\n"

It worked :slight_smile:

I was anticipating some function. Not sure what this line is doing:

${STRING%SUBSTRING} removes SUBSTRING from the end of STRING
\n : the \ must be escaped by \ so thats why i wrote \\n

1 Like