Remove trailing new line from 'clipboard' to prevent execution on the terminal?

Good modern terminals support bracketed paste: when you use the terminal's paste command, it sends special escape sequences around the clipboard content. If your shell supports bracketed paste, it'll paste the clipboard content including any control characters as-is, and in particular a trailing newline will not trigger the execution of the command.

Zsh ≥5.1 supports bracketed paste and has it on by default. Older versions can be taught. Bash ≥4.4 supports bracketed paste if you add set enable-bracketed-paste on to ~/.inputrc.

If your terminal or shell doesn't support bracketed paste, you could define a shell function that pastes without the trailing newline.

In zsh, the following command recalls the content of the clipboard, minus trailing newlines, and brings it up for editing (even if there are multiple lines):

print -z -- "`xsel -b`"

In bash, you can push the content of the clipboard minus trailing newlines to the history stack. After this, press Up to bring up the command for editing.

history -s -- "`xsel -b`"

Yes, use cat to paste in a file, do whatever you like and then execute it.

For this case:

$ cat > tmp
[paste][Ctrl+D]
$ tr -d '\n' <tmp | xclip # this remove all "new line" characters 
                          #  and copy back to clipboard

This makes \ev insert the clipboard without newlines in bash 4.0 and later:

pasteline() {
  local input=$(xsel -b)
  input=${input//$'\n'}
  READLINE_LINE=${READLINE_LINE:0:$READLINE_POINT}$input${READLINE_LINE:$READLINE_POINT}
  READLINE_POINT=$((READLINE_POINT+${#input}))
}

bind -x '"\ev": pasteline'

Replace xsel -b with pbpaste and install bash 4 in OS X.