chomp for bash

Bash doesn't have chomp command but you can use tr to get rid of \n \r (and other) characters.

  • http://www.blogvista.it realtebo

    Hei ! This simple post helpd me so much ! Thanx, i was blocked at writing a bash single-line command because I cannot eliminate spaces beetween two strings… whit

    tr -t [:space:]

    i resolved !

  • flipouk

    Thanks for the tip. Excellent!

  • Ed Greenberg

    Helped me too. I had a curl that returned a single line, but curl adds a linefeed that I didn’t need. It’s easy to deal with in perl, but I’m working in shell today.

    A=`curl http://foo`
    B=`echo $A|tr -d \\n`

    Thanks.

  • Ganneff

    A little late, but why not simply use bash itself for this? It is powerful enough, no need to have an external tool called:

    var1=”something with
    space and

    linefeeds”
    var1=${var1//
    /_}

    echo $var1

    (I only used _ to make it more visible here)