LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-15-2010, 02:25 PM   #1
statquant
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Rep: Reputation: 0
Question 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
 
Old 07-15-2010, 02:35 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
 
Old 07-15-2010, 02:42 PM   #3
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
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

Last edited by GrapefruiTgirl; 07-15-2010 at 02:44 PM. Reason: put backslash in front of > character -- thanks Mensa*
 
Old 07-15-2010, 03:35 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.
 
Old 07-15-2010, 03:39 PM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
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).
 
Old 07-15-2010, 04:07 PM   #6
rdgreenlaw
Member
 
Registered: May 2007
Location: Newport, Maine, USA
Distribution: Debian 8.7
Posts: 73

Rep: Reputation: 18
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.
 
1 members found this post helpful.
Old 07-15-2010, 04:14 PM   #7
rdgreenlaw
Member
 
Registered: May 2007
Location: Newport, Maine, USA
Distribution: Debian 8.7
Posts: 73

Rep: Reputation: 18
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.
 
Old 07-15-2010, 05:55 PM   #8
statquant
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
  


Reply

Tags
bash, prompt


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
New Year's gift for you: A variable-length bash prompt David the H. Linux - General 9 03-21-2014 06:04 AM
terminal prompt different from console prompt dh2k Slackware 5 03-29-2010 04:38 PM
how to generate variable length packets in iperf rohit83.ken Linux - Networking 1 03-10-2009 08:53 PM
Bash read in variable length text records lynx81191 Programming 4 11-17-2007 08:53 PM
Variable length objects kamransoomro84 Programming 4 10-28-2004 12:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration