Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Gnome Terminal allows to have different profiles. Is it possible to get the profile name under which that terminal started from the command line?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

This doesn't seem to be possible, however you can find out the default terminal title, so all you need to do to distinguish between tabs or profiles is to configure different default titles.

The control sequence ESC [ 2 1 t asks the terminal to insert its title on the terminal input stream. (See Xterm control sequences for more information about escape sequences for xterm and similar programs). Specifically, the terminal (if it supports this feature; gnome-terminal does) responds with ESC ] l title ESC \.

Here's a bash function that sets the variable whose name is passed as the first argument to the xterm title, if available. It times out after one second if the terminal doesn't support the feature, and returns a non-zero error code.

read_xterm_title () {
  # Clear IFS so that read doesn't do any word splitting.
  local IFS= read_xterm_title_header=
  # $1 is expected to be a parameter name. Do a crude format check.
  if [[ $1 = '' || $1 = *[!0-9_A-Za-z]* ]]; then return 120; fi
  eval $1=
  # Expect "ESC ] l title ESC \\"
  read -p $'\e[21t' -s -t 1 -r -n 3 read_xterm_title_header &&
  [[ $read_xterm_title_header = $'\e]l' ]] &&
  read -p '' -s -t 1 -r -d $'\e' $1 &&
  read -s -t 1 -r -n 1
}
read_xterm_title title
case $title in ...
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.