Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to strip newline character from input string in pasteboard

Tags:

shell

unix

macos

Hopefully fairly straightforward, to explain the use case when I run the following command (OS X 10.6):

$ pwd | pbcopy

the pasteboard contains a newline character at the end. I'd like to get rid of it.

like image 725
Andrew L Avatar asked Aug 14 '10 06:08

Andrew L


3 Answers

pwd | tr -d '\n' | pbcopy

like image 66
grep Avatar answered Sep 28 '22 20:09

grep


printf $(pwd) | pbcopy

or

echo -n $(pwd) | pbcopy

Note that these should really be quoted in case there are whitespace characters in the directory name. For example:

echo -n "$(pwd)" | pbcopy
like image 29
Dennis Williamson Avatar answered Sep 28 '22 19:09

Dennis Williamson


I wrote a utility called noeol to solve this problem. It pipes stdin to stdout, but leaves out the trailing newline if there is one. E.g.

pwd | noeol | pbcopy

…I aliased copy to noeol | pbcopy.

Check it out here: https://github.com/Sidnicious/noeol

like image 35
s4y Avatar answered Sep 28 '22 19:09

s4y