Get the name of the current terminal

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← Console managementHomeFixing the display with reset →

The tty command display the file name of the terminal connected to standard input. Type the following command:

tty

Sample outputs:

/dev/pts/0

OR

/dev/tty1

In this example, tar command will run, only if standard input is a terminal. Create a shell script called termtest.sh:

#!/bin/bash
# termtest.sh: Run the tar command only if command run from a termina
tty -s
status=$?
 
if [ $status -eq 0 ]
then
	echo "Running backup using tar command..."
	# tar cvf /dev/st0 /home
else
	logger "$0 must run from a terminal"
fi

Save and close the file. Run it as follows:

chmod +x termtest.sh
./termtest.sh
← Console managementHomeFixing the display with reset →