Skip to content

Instantly share code, notes, and snippets.

@chasballew
Last active August 29, 2015 14:10
Show Gist options
  • Save chasballew/72110106225f45a85892 to your computer and use it in GitHub Desktop.
Save chasballew/72110106225f45a85892 to your computer and use it in GitHub Desktop.
Current dir name in bash terminal

Bash provides an environment variable called PROMPT_COMMAND. The contents of this variable are executed as a regular Bash command just before Bash displays a prompt[0]. You can use the output of regular commands in PROMPT_COMMAND. Here we use echo to construct the command to set the window title.

export PROMPT_COMMAND='echo -ne "\033]0;${PWD##*/}\007"'

The key sequence here is:
ESC]0;stringBEL -- Set icon name and window title to $string

Breaking it down:
\033 - octal for ESC (escape)
\007 - octal for BEL (bell)
$PWD - outputs current directory
${var##Pattern} - Remove from $var the longest part of $Pattern that matches the front end of $var[1].
echo -n - Do not output the trailing newline.
echo -e - Enable interpretation of backslash-escaped characters

[0] http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html
[1] http://tldp.org/LDP/abs/html/parameter-substitution.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment