1

I'm trying to write a function that copies STDIN to the clipboard using pbcopy but also removes trailing newlines:

cbc () {
    echo -n "${1}" | pbcopy
}

But when I run cbc $PWD the process just hangs. What am I missing?

When run directly in the shell I get

$ echo "foo" | pbcopy
-bash: fork: Resource temporarily unavailable

If it doesn't hang.

EDIT: @Kusalananda got it. Forgot to unset another relevant alias... Thanks all!

With set -x enabled, (I'm new to BASH, so I put that inside the function -- how do I unset it? EDIT: figured that out quickly enough), I get

Davids-MBP-4:5_9_17_11am David$ cbc "foo"
+ pbcopy
+ set -x
+ echo -n foo
+ echo -n ''
+ pbcopy
+ set -x
+ echo -n ''
+ pbcopy
+ set -x
+ echo -n ''
+ pbcopy
+ set -x
+ echo -n ''
+ pbcopy
+ set -x
+ echo -n ''
+ pbcopy
+ set -x
+ echo -n ''
+ pbcopy
+ set -x
+ echo -n ''
+ pbcopy
+ set -x
+ echo -n ''
+ pbcopy
+ set -x
+ echo -n ''
+ pbcopy
+ set -x
+ echo -n ''
+ pbcopy
...
-bash: fork: Resource temporarily unavailable
+ echo -n ''
++ update_terminal_cwd
++ local url_path=
++ local i ch hexch LC_CTYPE=C LC_ALL=
++ (( i = 0 ))
++ (( i < 55 ))
++ ch=/
++ [[ / =~ [/._~A-Za-z0-9-] ]]
++ url_path+=/
++ (( ++i ))
++ (( i < 55 ))
++ ch=U
++ [[ U =~ [/._~A-Za-z0-9-] ]]
++ url_path+=U
++ (( ++i ))
++ (( i < 55 ))
++ ch=s
++ [[ s =~ [/._~A-Za-z0-9-] ]]
++ url_path+=s
++ (( ++i ))
++ (( i < 55 ))
++ ch=e
++ [[ e =~ [/._~A-Za-z0-9-] ]]
++ url_path+=e
++ (( ++i ))
++ (( i < 55 ))
++ ch=r
++ [[ r =~ [/._~A-Za-z0-9-] ]]
++ url_path+=r
++ (( ++i ))
++ (( i < 55 ))
++ ch=s
++ [[ s =~ [/._~A-Za-z0-9-] ]]
++ url_path+=s
++ (( ++i ))
++ (( i < 55 ))
++ ch=/
++ [[ / =~ [/._~A-Za-z0-9-] ]]
++ url_path+=/
++ (( ++i ))
++ (( i < 55 ))
++ ch=D
++ [[ D =~ [/._~A-Za-z0-9-] ]]
++ url_path+=D
++ (( ++i ))
++ (( i < 55 ))
++ ch=a
++ [[ a =~ [/._~A-Za-z0-9-] ]]
++ url_path+=a
++ (( ++i ))
++ (( i < 55 ))
++ ch=v
++ [[ v =~ [/._~A-Za-z0-9-] ]]
++ url_path+=v
++ (( ++i ))
++ (( i < 55 ))
++ ch=i
++ [[ i =~ [/._~A-Za-z0-9-] ]]
++ url_path+=i
++ (( ++i ))
++ (( i < 55 ))
++ ch=d
++ [[ d =~ [/._~A-Za-z0-9-] ]]
++ url_path+=d
++ (( ++i ))
++ (( i < 55 ))
++ ch=/
++ [[ / =~ [/._~A-Za-z0-9-] ]]
++ url_path+=/
++ (( ++i ))
++ (( i < 55 ))
++ ch=D
++ [[ D =~ [/._~A-Za-z0-9-] ]]
++ url_path+=D
++ (( ++i ))
++ (( i < 55 ))
++ ch=o
++ [[ o =~ [/._~A-Za-z0-9-] ]]
++ url_path+=o
++ (( ++i ))
++ (( i < 55 ))
++ ch=c
++ [[ c =~ [/._~A-Za-z0-9-] ]]
++ url_path+=c
++ (( ++i ))
++ (( i < 55 ))
++ ch=u
++ [[ u =~ [/._~A-Za-z0-9-] ]]
++ url_path+=u
++ (( ++i ))
++ (( i < 55 ))
++ ch=m
++ [[ m =~ [/._~A-Za-z0-9-] ]]
++ url_path+=m
++ (( ++i ))
++ (( i < 55 ))
++ ch=e
++ [[ e =~ [/._~A-Za-z0-9-] ]]
++ url_path+=e
++ (( ++i ))
++ (( i < 55 ))
++ ch=n
++ [[ n =~ [/._~A-Za-z0-9-] ]]
++ url_path+=n
++ (( ++i ))
++ (( i < 55 ))
++ ch=t
++ [[ t =~ [/._~A-Za-z0-9-] ]]
++ url_path+=t
++ (( ++i ))
++ (( i < 55 ))
++ ch=s
++ [[ s =~ [/._~A-Za-z0-9-] ]]
++ url_path+=s
++ (( ++i ))
++ (( i < 55 ))
++ ch=/
++ [[ / =~ [/._~A-Za-z0-9-] ]]
++ url_path+=/
++ (( ++i ))
++ (( i < 55 ))
++ ch=U
++ [[ U =~ [/._~A-Za-z0-9-] ]]
++ url_path+=U
++ (( ++i ))
++ (( i < 55 ))
++ ch=W
++ [[ W =~ [/._~A-Za-z0-9-] ]]
++ url_path+=W
++ (( ++i ))
++ (( i < 55 ))
++ ch=/
++ [[ / =~ [/._~A-Za-z0-9-] ]]
++ url_path+=/
++ (( ++i ))
++ (( i < 55 ))
++ ch=2
++ [[ 2 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=2
++ (( ++i ))
++ (( i < 55 ))
++ ch=0
++ [[ 0 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=0
++ (( ++i ))
++ (( i < 55 ))
++ ch=1
++ [[ 1 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=1
++ (( ++i ))
++ (( i < 55 ))
++ ch=6
++ [[ 6 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=6
++ (( ++i ))
++ (( i < 55 ))
++ ch=-
++ [[ - =~ [/._~A-Za-z0-9-] ]]
++ url_path+=-
++ (( ++i ))
++ (( i < 55 ))
++ ch=7
++ [[ 7 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=7
++ (( ++i ))
++ (( i < 55 ))
++ ch=/
++ [[ / =~ [/._~A-Za-z0-9-] ]]
++ url_path+=/
++ (( ++i ))
++ (( i < 55 ))
++ ch=s
++ [[ s =~ [/._~A-Za-z0-9-] ]]
++ url_path+=s
++ (( ++i ))
++ (( i < 55 ))
++ ch=p
++ [[ p =~ [/._~A-Za-z0-9-] ]]
++ url_path+=p
++ (( ++i ))
++ (( i < 55 ))
++ ch=r
++ [[ r =~ [/._~A-Za-z0-9-] ]]
++ url_path+=r
++ (( ++i ))
++ (( i < 55 ))
++ ch=i
++ [[ i =~ [/._~A-Za-z0-9-] ]]
++ url_path+=i
++ (( ++i ))
++ (( i < 55 ))
++ ch=n
++ [[ n =~ [/._~A-Za-z0-9-] ]]
++ url_path+=n
++ (( ++i ))
++ (( i < 55 ))
++ ch=g
++ [[ g =~ [/._~A-Za-z0-9-] ]]
++ url_path+=g
++ (( ++i ))
++ (( i < 55 ))
++ ch=/
++ [[ / =~ [/._~A-Za-z0-9-] ]]
++ url_path+=/
++ (( ++i ))
++ (( i < 55 ))
++ ch=5
++ [[ 5 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=5
++ (( ++i ))
++ (( i < 55 ))
++ ch=9
++ [[ 9 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=9
++ (( ++i ))
++ (( i < 55 ))
++ ch=9
++ [[ 9 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=9
++ (( ++i ))
++ (( i < 55 ))
++ ch=/
++ [[ / =~ [/._~A-Za-z0-9-] ]]
++ url_path+=/
++ (( ++i ))
++ (( i < 55 ))
++ ch=5
++ [[ 5 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=5
++ (( ++i ))
++ (( i < 55 ))
++ ch=_
++ [[ _ =~ [/._~A-Za-z0-9-] ]]
++ url_path+=_
++ (( ++i ))
++ (( i < 55 ))
++ ch=9
++ [[ 9 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=9
++ (( ++i ))
++ (( i < 55 ))
++ ch=_
++ [[ _ =~ [/._~A-Za-z0-9-] ]]
++ url_path+=_
++ (( ++i ))
++ (( i < 55 ))
++ ch=1
++ [[ 1 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=1
++ (( ++i ))
++ (( i < 55 ))
++ ch=7
++ [[ 7 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=7
++ (( ++i ))
++ (( i < 55 ))
++ ch=_
++ [[ _ =~ [/._~A-Za-z0-9-] ]]
++ url_path+=_
++ (( ++i ))
++ (( i < 55 ))
++ ch=1
++ [[ 1 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=1
++ (( ++i ))
++ (( i < 55 ))
++ ch=1
++ [[ 1 =~ [/._~A-Za-z0-9-] ]]
++ url_path+=1
++ (( ++i ))
++ (( i < 55 ))
++ ch=a
++ [[ a =~ [/._~A-Za-z0-9-] ]]
++ url_path+=a
++ (( ++i ))
++ (( i < 55 ))
++ ch=m
++ [[ m =~ [/._~A-Za-z0-9-] ]]
++ url_path+=m
++ (( ++i ))
++ (( i < 55 ))
++ printf '\e]7;%s\a' file://Davids-MBP-4.home/Users/David/Documents/UW/2016-7/spring/599/5_9_17_11am

closed as off-topic by Jesse_b, thrig, DopeGhoti, Rui F Ribeiro, slm May 4 '18 at 22:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Jesse_b, DopeGhoti, Rui F Ribeiro, slm
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Works for me. What do you see if you run it with set -x enabled? – Jesse_b May 4 '18 at 13:20
  • 2
    Works for me (High Sierra). Does it also block if you run the pipe directly in the shell? – nohillside May 4 '18 at 13:20
  • Also that won't remove trailing newlines, only prevent echo from adding one. – thrig May 4 '18 at 13:50
  • ... and it’s not looking at stdin; it’s looking at $1. And there’s no reason to say "${1}" in this context; just plain "$1" is perfectly good. – G-Man May 4 '18 at 20:54
  • 1
    If you use command pbcopy instead of just pbcopy in the function, you force the shell to not use pbcopy as a function or alias. – Kusalananda May 5 '18 at 14:27

Browse other questions tagged or ask your own question.