10

I'm trying to write a simple alias on my Mac OS X terminal to copy the current working directory. I have this:

alias cpwd="echo \`pwd\` | pbcopy; echo \"Copied \`pwd\`\""

Then I can just run the following to copy it:

$ cpwd

Problem is that echo pwd includes the newline at the end. So when I paste it, it executes immediately (if pasted in a terminal).

All I want to do is strip off the trialing newline, but nothing I find on the internet seems to work for me. Seen various solution involving sed, awk, and cut, but I can't quite get it. Seems like it would be easy to do.

2 Answers2

7

I belive this should work :

alias cwd="echo -n `pwd` | pbcopy; echo \"Copied `pwd`\""

The -n says "no new line". Either that or you can always pass the output through tr and remove the new line character like that:

alias cwd="echo `pwd` | tr -d "\n" | pbcopy; echo \"Copied `pwd`\""

I'm not sure if you want to remove the trailing new line char from the first echo or from both - but i guess you can figure it out if it will work for the first one ;)

mnmnc
  • 4,257
1

I'm not sure about Mac OS X echo command but if -n argument is provided echo will not output the trailing newline:

-n do not output the trailing newline

Regards...