Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...
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.
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*
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).
#!/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.
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.
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 !!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.