I'm looking for a way to force my PS1 variable to a certain number of chars.
It currently looks like:

$PS1="\W $"

and I'm trying to keep the same number of characters regardless of the length of current folder name.

For example, both Documents and bin would be displayed as 10 character strings, keeping the left side of my terminal the same width all of the time.

  • How are you going to format this? Something like "LongFileName" -> "LongFileNa"? By the way, awk can do this: echo 12345 | awk '{substr($0, 1, 10)}'. How to stuff it into $PS1, I have no idea. – new123456 Jun 22 '11 at 21:03
  • @new123456 this is exactly my issue. I can do what you're saying, but if you use $pwd you'll only get your initial login directory, it won't update as you change directories – cmwright Jun 23 '11 at 14:20
  • bash has the PROMPT_COMMAND variable, but it doesn't have the same semantics as PS1 (notably the escape characters). zsh can do this with PROMPT (it evaluates at every run). – new123456 Jun 23 '11 at 15:11

If you are using an xterm or similar then you can put the current directory in the Title Bar and then have a fixed prompt

PS1='\[\e]0;\h \w\007\] \u$ '

will put the current hostname and directory in the title bar and make the prompt username$ e.g. iain$

There is more information here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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