Skip to main content
chomp(str) removes trailing newline characters (\n, \r\n, and \r) from the end of a string. It does not affect newlines in the middle of the string.

Signature

Example

Read a version file and strip the trailing newline before using it in a command:
file("VERSION") reads the file contents including any trailing newline. chomp removes it so the version is clean for use in the git tag.

Notes

  • chomp removes only trailing newlines, not spaces or tabs. Use trimspace to strip all whitespace.
  • If the string does not end with a newline, it is returned unchanged.