Com o que wjandrea disse, é correto que você possa pegar o stderr que a leitura está fazendo. Infelizmente, tentar ler com stderr não mantém a variável key , portanto, a saída para echo $key terminará em branco. No entanto, em um script, ele manterá a variável porque a própria linha de leitura não está sendo redirecionada de stderr para stdout 2>&1 no script. Em vez disso, o script consegue manter a variável porque agora está definida antes de chegar ao redirecionamento. Acho que para facilitar, use echo lines. Eu adicionei todos os exemplos.
Exemplo do seu script:
:~$ cat test1.sh
#!/bin/bash
read -p "type a key and Enter: " key
echo "the key was $key"
:~$ ./test1.sh | tee junk
type a key and Enter: G
the key was G
:~$ cat junk
the key was G
Com redirecionamento 2>&1 :
:~$ ./test1.sh 2>&1 | tee junk
type a key and Enter: G
the key was G
:~$ cat junk
type a key and Enter: the key was G
Com echo line:
:~$ cat test.sh
#!/bin/bash
echo -n "Type a key and press Enter: "
read key
echo "the key was $key"
echo line com o comando tee:
:~$ ./test.sh | tee junk
Type a key and press Enter: X
the key was X
:~$ cat junk
Type a key and press Enter: the key was X
Exemplo do script não:
com stderr :
:~$ read -p "Type a key and press Enter: " key 2>&1 |tee junk; echo "The key was $key" | tee -a junk
Type a key and press Enter: F
The key was
:~$ cat junk
Type a key and press Enter: The key was
Com echo e vários comandos tee:
:~$ echo -n "Type a key and press Enter: " | tee junk; read key; echo "The key was $key" | tee -a junk
Type a key and press Enter: F
The key was F
:~$ cat junk
Type a key and press Enter: The key was F
Espero que isso ajude!