Redirecting terminal to variable

when i do something like this:

bona=$(echo hi2 > /dev/pts/1 ; printf '%s\n' "" | sed '/^$/d')

i get:

hi2

and the $bona variable is empty, when I run:

echo ${bona}

i get the result "hi2" outside of the variable. I want it stored in the bona variable with nothing outputted to the screen until i echo out the bona variable. but the problem is that the bona variable is always empty.

how can i keep the hi2 in the bona variable so when i echo it out, it shows hi2?

i have tried the following, but to no avail:

bona=$(echo hi2 > /dev/pts/1 >&1; printf '%s\n' "" | sed '/^$/d')

fyi, /dev/pts/1 is my active terminal that im working out of. it needs to be specified.

I really have no idea what you are trying to achieve.

printf '%s\n' "" | sed '/^$/d'

is of course always empty and therefore bona will always be empty.

Try:

bona=hi2
echo "$bona"

--
Perhaps you mean something like this?

bona=$(echo hi2 | tee /dev/tty)

tee to terminal might not be the solution as s/he wants "it stored in the bona variable with nothing outputted to the screen".

What does "i get the result "hi2" outside of the variable" mean?

1 Like

I run a couple of my scripts through pipes and i need to know the terminal from which a script is running.

commands like:

who am i | sh

does not produce the expected terminal address...i.e. /dev/pts/1

So, im looking for a way to identify the most recently active terminal by sending data to it.

i feel this has to be doable.

who am i | sh

Means execute the output of who am i in new shell as if it were a command, so the result is going to be an error.

Would sth. like

lsof -p$$ | awk '/ 1u/ {print $9}'
/dev/pts/2

or

ps | awk -vSELF=$$ 'match ($4, SELF) {print $13}'
pts/2

help?

1 Like

Usually /dev/tty would be interchangeable with the device of the current terminal, have you given this a go?

or add /dev/ to the front of who am i output:

$ who am i | awk '{print "/dev/"$2}'
/dev/pts/4