1

I'm working in an HPC cluster which has a lot of partitions ($HOME, $SCRATCH, $WORKDIR, $SHARED,...) and was trying to find a way to clearly identify which partition I'm at the moment.

After some digging, I found that

export PS1="\e[33;1m\u@\h: \e[31m\w\e[0m\$ "

would show me what I'm looking for. However, the path becomes really cumbersome since all of these partitions have a really long pwd output.

I found out that this command,

aaa@bbb: ~$ echo $PROMPT_COMMAND
printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" ${PWD/#$HOME/~}"

mainly the ${PWD/#$HOME/~} part, might be why the $HOME directory appears simply with ~.

Is there a way to change this environment variable (or any other alternative) for me to have something like:

aaa@bbb: ~/foo/bar$          # when I'm working in $HOME
aaa@bbb: scratch/foo/bar$    # when I'm working in $SCRATCH
aaa@bbb: shared/foo/bar$     # when I'm working in $SHARED
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • You need to do it in multiple commands: `dir=${PWD/old1/new1}; dir=${dir/old2/new2}; dir=${dir/old3/new3}; ...` and then use `$dir` in the prompt – Barmar Feb 11 '22 at 01:41
  • 1
    BTW, you should be `\[` and `\]` around escape sequences, so bash knows that they don't take up space. – Barmar Feb 11 '22 at 01:44
  • You could use `\W` instead of `\w`. This will show the last component of the directory, instead of the whole directory. But you won't be able to distinguish `scratch/foo/bar` from `shared/foo/bar` because it will just show `bar` – Barmar Feb 11 '22 at 01:45
  • As I understand it $PROMPT_COMMAND is just the prompt that is printed at the begining of each line of the terminal see https://stackoverflow.com/questions/3058325/what-is-the-difference-between-ps1-and-prompt-command –  Feb 11 '22 at 01:48
  • @Barmar , thank you for your answers. I didn't understand how to implement you first comment. – import numpy as np Feb 11 '22 at 11:46
  • `PROMPT_COMMAND` is a piece of code to execute immediately before displaying the prompt. It is typically used to set the value of `PS1`, and so is useful in allowing the value to be built up piece by piece, rather than having one giant expression containing embedded code. – chepner Feb 11 '22 at 14:33
  • Note that there's no reason to export `PS1`; it's value is only useful to shells that already source `.bashrc` on startup. Keep it an ordinary shell variable to avoid cluttering the environment that gets inherited by all child processes. – chepner Feb 11 '22 at 14:35

2 Answers2

1

Set PROMPT_COMMAND to command, which will set a variable with truncated path value.

Replace \w in PS1 with the variable name.

My example:

export PROMPT_COMMAND='export CURDIR=$( pwd | sed "s|$HOME|~|;s|/var/lib/postgresql|pg|;s|/opt|stuff|" )'
export PS1='\[\033]0;\u@\h:\w\007\]\[\033[01;32m\]\u@\h\[\033[01;34m\] $CURDIR \$\[\033[00m\] '
user3132194
  • 2,381
  • 23
  • 17
1

In ~/.bashrc you can define :

PROMPT_DIRTRIM=3

to keep the last 3 compoments of $PWD

Philippe
  • 20,025
  • 2
  • 23
  • 32