0

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 ?

yanachen
  • 103
  • 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

2 Answers2

4

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

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
2

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

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255