LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Variable length console prompt (http://www.linuxquestions.org/questions/linux-general-1/variable-length-console-prompt-820048/)

statquant 07-15-2010 02:25 PM

Variable length console prompt
 
Hello everybody,
first thanks for taking the time to read this thread.
I have a question about the prompt... it is very easy to tune it for it to be colored and display path where you are etc...

But my problem is that when the path is too long I would prefer the code line to be on the folowing line...

Ex

11:00 me@host a/short/path > ls -ltr ./stuff
11:00 me@host a/very/very/very/long/path
> ls -ltr ./stuff

and to be honnest as I am very new in LINUX I don't know how to do this...

Would you kindly help on this ?
Cheers

MensaWater 07-15-2010 02:35 PM

Your base prompt is PS1 so you can set it by typing:
export PS1=<whatever>

So you can the greater than with:
export PS1=\>
(You need the \ in front of the sign because it is also a redirect character and the \ escapes it (tells the command to take it literally).

You might want to do something a little different like:
export PS1="[\u@\h \W]\$"

That gives you a shorter prompt that only includes your user name, hostname and the basename (last directory) of the path. It would look something like:
[user@host mqueue]$
If you were in /var/spool/mqueue for example.

The reason you might want user and hostname in the prompt is to be sure you're doing something as the user you think you are on the host you think you are on. Many an admin has accidentally killed their system by doing something as root that they thought they were doing as a non-privileged user or doing it on a host other than the one they thought they were on.

GrapefruiTgirl 07-15-2010 02:42 PM

The trouble here is, whatever code snippet you use in your .bashrc or .bash_profile file to create your custom prompt, it is only executed once, when the terminal is started. So, scripting a prompt that way won't work (beyond the first time).

You could terminate your path with a \n (a newline) regardless how long the path is, but I would understand if you said that that would not be too pleasing. I wouldn't prefer that either.

The best solution I know of (and there certainly could be another way!), would be to use the $PROMPT_DIRTRIM variable, which is a bash built-in, and which determines how many elements (separated by slashes) will be shown as the path in your prompt. So, as an example:

export PS1="\w \>"

The above will show the current working directory in your prompt. If you are 7-deep in subfolders, you'll have a
"very/very/very/very/very/long/path >"

But: set the $PROMPT_DIRTRIM variable, let's say you set it to equal 3, and then you will have:
"/.../very/long/path >" -- you see, you now have /.../ followed by only 3 folders/in/the/path.


If you want to do this so it has effect all the time, just set the variable in /etc/profile or your ~/.profile so it will take effect at startup/login.

Hope this helps -- but if there's a way to dynamically alter the prompt as you describe, putting a newline at the end only when the path is very long, I'll be interested to learn of it.

Sasha

MensaWater 07-15-2010 03:35 PM

Quote:

scripting a prompt that way won't work (beyond the first time)
I assume you meant that for the OP rather than my last example as it most assuredly works as I've been using it for many years.

GrapefruiTgirl 07-15-2010 03:39 PM

Yes, it was for the OP. For example:

export PS1="$(date)"

will now show the date as a prompt, but it shows the exact same date+time each time the prompt is shown, in the same console instance; the date/time doesn't advance. When one opens a new console, the date will again be fresh at first, but will not be updated further within the same console (i.e. it's not dynamic within a console instance).

rdgreenlaw 07-15-2010 04:07 PM

With thanks to The Linux Online website:

Create a shell script (You must be using BASH)

Code:

#!/bin/bash

# Filename: addcr

pwd_len=20        # Default maximum length before prompt goes to next line

if [ $1 -gt 0 ]
  then
    pwd_len=$1    # Override default if a command line parameter is given
if

dir=`pwd`
if [ ${#dir} -gt $pwd_len ]
  then
    echo ""        # Makes the cursor go to the next line
    echo "      "  # spaces in a bit
fi

Place the script somewhere in your system (/usr/bin might be good.)

Then edit your .bashrc to include `/usr/bin/addcr` for the default length of 20 characters
or `/usr/bin/addcr #` for any other value (replace # with any number you wish) immediately before the \$ in PS1


My .bashrc contains 3 lines that set the value of PS1. These would each need to be altered if the prompt is to be effective for any terminal. Otherwise some terminals will see the change while others simply use the original prompt.

rdgreenlaw 07-15-2010 04:14 PM

Wrapping any command in a shell script, then executing the shell script by placing it in backticks (see my other post) makes it possible for anything to be dynamic including displaying the date using $(date).

The Linux Online website (http://www.linuxonline.com) has a very complete section on programming the bash prompt which includes using bash scripts called at the time the prompt is displayed. This makes the prompt dynamically alterable.

I didn't know how easily it could be accomplished until I looked for an answer to statquant's question.

statquant 07-15-2010 05:55 PM

Wahhhhhhhhhhhhoooooooooo awesome !!!
thank you guys for all this, I was offline 3 hours and I come back with the solution to my problem.
I am trying this now... keep you posted !!

Cheers


All times are GMT -5. The time now is 05:30 AM.