How do I fix /dev/stderr pointing to /dev/null instead of current terminal?

Posted on

Problem :

How do fix when /dev/stderr is pointing to /dev/null, because of which, all err messages are redirected to /dev/null without using the > operation.

~  tty
/dev/pts/0
 ~  l /dev/stdout
lrwxrwxrwx 1 root root 15 Jun  1 18:06 /dev/stdout -> /proc/self/fd/1
 ~  l /dev/stdin
lrwxrwxrwx 1 root root 15 Jun  1 18:06 /dev/stdin -> /proc/self/fd/0
 ~  l /proc/self/fd/0
lrwx------ 1 avirukbasak avirukbasak 64 Jun  1 18:20 /proc/self/fd/0 -> /dev/pts/0
 ~  l /proc/self/fd/2
l-wx------ 1 avirukbasak avirukbasak 64 Jun  1 18:20 /proc/self/fd/2 -> /dev/null
 ~  l /proc/self/fd/1
lrwx------ 1 avirukbasak avirukbasak 64 Jun  1 18:20 /proc/self/fd/1 -> /dev/pts/0

An image of the terminal is also attached here

Solution :

Use exec to change the current shell’s file descriptors:

exec 2>/dev/tty

Use command -v to check whether a program is installed:

if [ -t 0 ]; then
    if command -v zsh >/dev/null 2>&1; then
        zsh
    fi
fi