Strip newline from piped input

I’ve been setting up a script to work with haste-server. This takes piped or file input (tail /var/log/messages | haste and haste < /path/to/file.txt) and submits it to the server which then outputs a in my terminal. See below:

key="$(curl --silent --data-binary @/dev/fd/0 $url/documents | cut -d """ -f 4)"
echo "$url/$key"

It works just fine, however it adds a trailing new line to the input. How can I read @/dev/fd/0 to remove the n new line?

Edit: Here is my completed script for submitting a haste that trims the newline:

#!/usr/bin/env bash

data=$(< /dev/fd/0)
key="$(printf "%s" "$data" | curl -X POST -s --data-binary @- "$url/documents" | cut -d "\"" -f 4)"
echo "$url/$key"
Asked By: Nahydrin


Avoiding newlines by ‘echo’ command

Instead of echo, use echo -n.

If that does not work (e.g. on OSX with /bin/sh as shell), or if you want make your script independent of which shell it runs under, use /bin/echo -n .

Avoiding newlines from "payload" (here: ‘$key’)

Change the output newlines with tr, e.g.

echo "$url/$key" | tr 'n' '|'
Answered By: village
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.