3

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 ?

5
  • 3
    What do you call bash tty clipboard? Is that the readline kill ring, the thing you paste with CTRL+Y in emacs mode? Jul 25, 2016 at 9:18
  • @StéphaneChazelas bash tty ie non X terminal emulator. The bash that pops off when X server isn't installed. Jul 25, 2016 at 9:47
  • bash is just a command line interpreter that can run in a terminal or not. When running in a terminal, it has its own line editor using the readline library. That line editor has a kill ring which keeps track of what you have erased or copied in the session using things like ^W, ^U, ^K and so on and which you can yank with ^Y (assuming emacs mode)? Is that what you're talking about? Or are you refering to the copy-pasting handled by gpm in Linux virtual consoles (where you paste with the middle mouse button)? Jul 25, 2016 at 9:57
  • @StéphaneChazelas when you start arch linux you are welcomed with a black screen where you can enter commands. I'm looking to do what I described in my OP in that thing. Jul 25, 2016 at 10:56
  • It's understood now that you're talking of the Linux virtual consoles, but the question still stands: what clipboard buffer are you refering to in there? The Linux virtual console has no clipboard buffer. gpm, which may be enabled in there has one. bash, via readline has one. How do you paste with that "clipboard" of yours? Jul 25, 2016 at 11:00

2 Answers 2

4

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.

1
  • this is good, with a simple bracing function in bashrc I am able to edit all outputs easily now, real time saver when it comes to manipulating uuids and stuff. Jul 26, 2016 at 18:06
1

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.

If you're running inside a terminal multiplexer like tmux or screen, they both offer copy&paste. This is very useful, if you are on a server.

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 Enter, press <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
$EDITOR exec.sh
bash exec.sh
2
  • 1
    you talk about xclip but does it exist if X server isn't installed Jul 25, 2016 at 9:46
  • xclip requires an X server indeed, I just clarified that I meant "desktop environment" with "local environment".
    – kba
    Jul 25, 2016 at 9:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .