0

Why doesn't this work? There has to be a very simple answer to this.

I have two bash scripts. The first script calls the second which opens an editor on a tmp file, once user quits, cat the file which will be assigned to a variable in the first script.

test-use-editor

#!/bin/bash

test=$(use-editor)

echo $test

use-editor

#!/bin/bash

TMP_MSG="/tmp/tmp_msg"
$EDITOR  $TMP_MSG

cat $TMP_MSG
rm $TMP_MSG

If I call use-editor without assigning and $() the editor opens just fine, but if I try to assign it to $test it just hangs. The editor does start because I can see it in my processes, but it's in a subshell or something. How can I get it to open within the terminal which called the first script?

ram
  • 680
  • 5
  • 15
  • It works for me - I run `./use-editor`, Emacs opens up, I type `abcd`, exit Emacs, I see `abcd` printed – Arkadiusz Drabczyk Feb 27 '21 at 23:54
  • @ram If you are using a terminal based editor, then `$(use-editor)` may capture the editor UI instead of showing it on screen – that other guy Feb 28 '21 at 00:12
  • hmm. I had tried adding `./` before and it still wouldn't work. What's strange is if I just hard code `use-editor` into the first script, it works as expected. @ArkadiuszDrabczyk – ram Feb 28 '21 at 00:18
  • @thatotherguy I'm using neovim, so yes I am. I'm not sure why it works if the exact same code is in one file rather than two though. Any ideas of things I could try to get it to work in a separate script? – ram Feb 28 '21 at 00:20
  • 1
    It likely fails when you put `$EDITOR $TMP_MSG` inside the `$(..)` and works when you don't, regardless of the number of files. The hacky workaround would be `$EDITOR $TMP_MSG > /dev/tty` – that other guy Feb 28 '21 at 00:23

1 Answers1

1

You need to pass tty on to use-editor, as thatotherguy suggested.

test-use-editor

#!/usr/bin/env bash

test=$(./use-editor $(tty))

echo $test

use-editor

#!/usr/bin/env bash

TTY=${1:-/dev/tty}
TMP_MSG="/tmp/tmp_msg"
$EDITOR  $TMP_MSG < $TTY >& $TTY

cat $TMP_MSG
rm $TMP_MSG
Philippe
  • 20,025
  • 2
  • 23
  • 32