Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
General
cancel
Showing results for 
Search instead for 
Did you mean: 

set Window title using a script (gnome-terminal/RedHat AS4)

SOLVED
Go to solution
Christian Deutsch_1
Esteemed Contributor

set Window title using a script (gnome-terminal/RedHat AS4)

Hi folks,

I have been using a script for about 14 years on HP-UX to set window titles.

Now I switched to Linux. gnome-terminal sets TERM to xterm, so I'm trying to use my script to set the window (tab) title. But bash seems not to know the "print" command (see script below).

What do I use instead of the "print" command/builtin?

Helpful answers will be rewarded with points of course.

Thanks, Christian

#! /bin/sh
#
# Set the title bar and perhaps the icon for a window
# Usage: title Your Title Here
# title -i Icon Only
# title -w Window Only

function Main
{
# Decide on the type of terminal window they are using
# so we can emit the correct escape sequences.
case $TERM in
xterm*) window_type=x;;
hp*|26*|23*) window_type=hp;;
dtterm) window_type=dt;;
*) print -u2 "$0: What kind of TERM is $TERM\?"
return 1;;
esac

# Call the function to set both the title and icon or just the
# icon, depending on if they used the -i flag.
case "$1" in
-i) shift; set_icon $window_type "$@";;
-w) shift; set_title $window_type "$@";;
*) set_title $window_type "$@";
set_icon $window_type "$@";;
vi -c 1 /home/cdeutsch/bin/title------------------------
[cdeutsch@hesed tmp]$ wcl ~/bin/title
bash: wcl: command not found
[cdeutsch@hesed tmp]$ wc -l ~/bin/title
67 /home/cdeutsch/bin/title
[cdeutsch@hesed tmp]$ cat ~/bin/title
#! /bin/sh
#
# Set the title bar and perhaps the icon for a window
# Usage: title Your Title Here
# title -i Icon Only
# title -w Window Only

function Main
{
# Decide on the type of terminal window they are using
# so we can emit the correct escape sequences.
case $TERM in
xterm*) window_type=x;;
hp*|26*|23*) window_type=hp;;
dtterm) window_type=dt;;
*) print -u2 "$0: What kind of TERM is $TERM\?"
return 1;;
esac

# Call the function to set both the title and icon or just the
# icon, depending on if they used the -i flag.
case "$1" in
-i) shift; set_icon $window_type "$@";;
-w) shift; set_title $window_type "$@";;
*) set_title $window_type "$@";
set_icon $window_type "$@";;
esac
return 0
}

function set_title # set title bar only
{
case $1 in
x) shift; print '\033]0;'"${*}"'\c' # title bar only
# In xterm, '\033]0;'"${*}"'\033\c' sets icon AND title bar
;;
dt) shift; print '\033]0;'"${*}" # title bar only
;;
hp) shift; title="$@"
print '\033&f0k'${#title}D"$title"\\c
;;
esac
}

function set_icon # set icon label only
{
case $1 in
x) shift; print '\033]1;'"${*}"'\033\c'
;;
dt) shift; print '\033]1;'"${*}"
;;
hp) shift; title="${*}"
print '\033&f-1k'${#title}D"$title"\\c
;;
esac
}

# If there were no arguments, print a usage synopsis
if [ $# = 0 ]
then print -u2 "Usage: ${0##*/} Your Title Here"
print -u2 "or: ${0##*/} -i Icon Title Only"
print -u2 "or: ${0##*/} -w Window Title Only"
exit 1
else
Main "$@"
exit $?
fi
Yeshua loves you!
2 REPLIES
Mike Stroyan
Honored Contributor
Solution

Re: set Window title using a script (gnome-terminal/RedHat AS4)

Use echo -e instead of print to work with bash.
The bash echo command can use either "\c" or -n to suppress a newline.
You need to use >&2 instead of -u2 to send echo to stderr.
Your xterm escape sequence should end the title string with "\007".
You can set just the window title in gnome-terminal with "\033]2;$*\007"

$ diff -u title.orig title
--- title.orig 2006-11-09 14:24:56.000000000 -0700
+++ title 2006-11-10 09:17:30.000000000 -0700
@@ -13,7 +13,7 @@
xterm*) window_type=x;;
hp*|26*|23*) window_type=hp;;
dtterm) window_type=dt;;
-*) print -u2 "$0: What kind of TERM is $TERM\?"
+*) echo "$0: What kind of TERM is $TERM\?" >&2
return 1;;
esac

@@ -31,13 +31,13 @@
function set_title # set title bar only
{
case $1 in
-x) shift; print '\033]0;'"${*}"'\c' # title bar only
+x) shift; echo -e '\033]2;'"${*}"'\007\c' # title bar only
# In xterm, '\033]0;'"${*}"'\033\c' sets icon AND title bar
;;
-dt) shift; print '\033]0;'"${*}" # title bar only
+dt) shift; echo -e '\033]0;'"${*}" # title bar only
;;
hp) shift; title="$@"
-print '\033&f0k'${#title}D"$title"\\c
+echo -e '\033&f0k'${#title}D"$title"\\c
;;
esac
}
@@ -45,21 +45,21 @@
function set_icon # set icon label only
{
case $1 in
-x) shift; print '\033]1;'"${*}"'\033\c'
+x) shift; echo -e '\033]1;'"${*}"'\007\c'
;;
-dt) shift; print '\033]1;'"${*}"
+dt) shift; echo -e '\033]1;'"${*}"
;;
hp) shift; title="${*}"
-print '\033&f-1k'${#title}D"$title"\\c
+echo -e '\033&f-1k'${#title}D"$title"\\c
;;
esac
}

# If there were no arguments, print a usage synopsis
if [ $# = 0 ]
-then print -u2 "Usage: ${0##*/} Your Title Here"
-print -u2 "or: ${0##*/} -i Icon Title Only"
-print -u2 "or: ${0##*/} -w Window Title Only"
+then echo "Usage: ${0##*/} Your Title Here" >&2
+echo "or: ${0##*/} -i Icon Title Only" >&2
+echo "or: ${0##*/} -w Window Title Only" >&2
exit 1
else
Main "$@"
Christian Deutsch_1
Esteemed Contributor

Re: set Window title using a script (gnome-terminal/RedHat AS4)

Perfect! Thank you Mike!
Yeshua loves you!