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 ?
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
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.
How would I put the ls output inside the bash tty clipboard?
If running in a desktop environment, I prefer using the X11 clipboard:
somecommand | xclip -i
This way, the output can be pasted on the command line with the means of your terminal emulator, using
xclip -o and in every other application.
Lastly, there are means to manipulate the readline yank-ring or even sync it with the X11/OSX clipboard. The advantage is that you can use
Ctrl-Y in the prompt to paste. The disadvantage is that it's a this-bash-session-only solution.
how do I put the output of a command in the command prompt directly so I am free to edit it?
If you want to use the output of a command as an argument, use a subshell:
echo "ls output: $(somecommand)"
Then, before pressing
<Alt-Ctrl-e>. This will expand the subshell within the prompt so you can edit it to your liking.
If you want to execute the output of acommand, redirect it to a file, edit it and execute it. Interpreting input as shell commands can be very dangerous (e.g.
rm -rf /$var when
$var is empty), so better double check the file and keep it around.
somecommand > exec.sh