Bash – How to output into clipboard and paste in bash tty

bashclipboardtty

How would I put the ls output inside the bash tty clipboard and then how would I paste it in the command prompt ? Alternatively how do I put the output of a command in the command prompt directly so I am free to edit it ?

Best Answer

You can perhaps do what you want using bash's readline with -i which provides an initial input to -e edit. For example, using date rather than ls as it is simpler to see:

$ read -ei "$(date)" && $REPLY
Mon Jul 25 13:42:47 CEST 2016

You now have the string Mon Jul 25 13:42:47 CEST 2016 as shown with the cursor at the end. You can edit this using the usual cursor keys and so on. For example, you could edit the date 25 to 20 and then add an echo to the start, giving

echo Mon Jul 20 13:42:47 CEST 2016

When you press return, the line read is placed in variable REPLY, which you then execute. This is a bit fragile, as the reply is split on spaces and so on. You can add quotes and an eval:

$ read -ei "$(date)" && eval "$REPLY"

Then if you edit the line, changing 25 as before, and insert a command that needs an argument with spaces, eg:

date +%s -d 'Mon Jul 20 13:42:47 CEST 2016'

you will get the right answer 1469014967. As always, beware with eval.

Related Question