My Gnu/Linux system is red hat and I ssh it from xshell. The command prompt format is like this:

[xuemeng.cyn@e92e01494.em21 /home/qihang.gqh/online/h5_reco/user_level_1.0]
$vi dn_click.py

I remember that there is another format that the current working dictionary is in front of the command like this:

user_level_1.0# *****(command)

How can I switch the format ?

  • 2
    You're talking about the prompt? This is configured by setting environment variable PS1. There is tons of documentation here and elsewhere online about how to set this up. – B Layer Aug 16 '17 at 11:11
  • This indeed seems to be about the prompt. In bash, you'd want to customize PS1. For reference on the possible special characters, see info "(bash)Controlling the Prompt" (the "Controlling the Prompt" section of the bash info manual). The second format you refer to has a # which is frequently used in PS1 when the user has uid 0 (that is, for root shells). You could get the value of the variable $PS1 in a shell that has the prompt you want and use that in your $HOME/.bashrc. – njsg Aug 16 '17 at 11:17

If you want to see: (the last part of your current directory) followed by the pound sign, you would use:

PS1='\W# '

Put this line in the file .bashrc in your home directory.

Although I would caution you against using # in a non-root prompt, as it's customary to use something like > or $ in non-root prompt strings and # in root's prompt.

Reference: Bash shell prompting

| improve this answer | |

Here is a simple explanation about prompt env vars (taken from the bash man page):

PS0 – The value of this parameter is expanded like PS1 and displayed by interactive shells after reading a command and before the command is executed.

PS1 – The value of this parameter is expanded (see PROMPTING below) and used as the primary prompt string. The default value is \s-\v\$ .

PS2 – The value of this parameter is expanded as with PS1 and used as the secondary prompt string. The default is >

PS3 – The value of this parameter is used as the prompt for the select command

PS4 – The value of this parameter is expanded as with PS1 and the value is printed before each command bash displays during an execution trace. The first character of PS4 is replicated multiple times, as necessary, to indicate multiple levels of indirection. The default is +

and here some vars that you can use in your format:

\a: an ASCII bell character (07)

\d: the date in “Weekday Month Date” format (e.g., “Tue May 26”)

\D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required

\e : an ASCII escape character (033)

\h : the hostname up to the first ‘.’

\H : the hostname

\j : the number of jobs currently managed by the shell

\l: the basename of the shell’s terminal device name

\n : newline

\r : carriage return

\s : the name of the shell, the basename of $0 (the portion following the final slash)

\t : the current time in 24-hour HH:MM:SS format

\T : the current time in 12-hour HH:MM:SS format

\@ : the current time in 12-hour am/pm format

\A : the current time in 24-hour HH:MM format

\u : the username of the current user

\v : the version of bash (e.g., 2.00)

\V : the release of bash, version + patch level (e.g., 2.00.0)

\w : the current working directory, with $HOME abbreviated with a tilde

\W : the basename of the current working directory, with $HOME abbreviated with a tilde

\! : the history number of this command

\# : the command number of this command

\$ : if the effective UID is 0, a #, otherwise a $

\nnn : the character corresponding to the octal number nnn

\\ : a backslash

\[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt

\] : end a sequence of non-printing characters

So: a simple example

PS1= "\h:\! \w \$"

Form more info you can see the bash man page

| improve this answer | |
  • When you copy something written by someone else, you must clearly indicate what you're citing and where it's from. – Gilles 'SO- stop being evil' Aug 16 '17 at 20:42
  • Added, this text is from the bash man page and is under the GNU Free Documentation License, the only part from myself is the example and it can be reproduced with any other restrictions than the GNU Free Documentation License – Christopher Díaz Riveros Aug 16 '17 at 21:30
  • This is an acknowledgement, not a citation. Also, your post violates the copyright, since the GFDL forbids redistributing in a way that removes the copyleft restrictions, but posts on this site are licensed more liberally. – Gilles 'SO- stop being evil' Aug 16 '17 at 21:50
  • ok, I'll keep it on mind, I'm new to all of this, but how should I cite a man page in order to not violate the copyright? Thanks for noticing it, I had no idea about that – Christopher Díaz Riveros Aug 16 '17 at 22:00
  • Cite only what's useful (e.g. here at most the description of PS1, \h and \w, not the whole thing) so that it's fair use instead of a copyright violation. Use quote markup (> …) and clearly state the source instead of pretending that you wrote it so that it isn't plagiarism. – Gilles 'SO- stop being evil' Aug 16 '17 at 22:18

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.