Search code examples
bashshellm4

Remove trailing newline from esyscmd in m4


I'm using m4 to create some basic macros and I realize that when using esyscmd there's a trailing new line added to a string when the command is run.

Example:

define(MY_HOSTNAME, esyscmd(`hostname'))
MY_HOSTNAME
Some other text...

Renders:

> my.host.name
>
> Some other text...

(complete with a trailing new line)

By adding dnl at the end of the define (or esyscmd) nothing appears to happen and there's still a trailing newline.

What's the best way to drop the trailing newline when calling esyscmd in m4?


Solution

  • *nix systems have tr by default. Make use of that:

    define(MY_HOSTNAME, esyscmd(sh -c "hostname | tr -d '\n'"))
    

    and you'd get rid of the trailing newline!