How do i change my prompt string in linux?
mountaing last edited by
Linux shells have a way of setting up your prompt string for the shell. When you log into a linux server as non privileged user, by default, you would see this:
This is the default shell prompt string one, i.e. PS1 environment variable.
However, there are multiple prompt strings:
PS1 Is the primary prompt string.
PS2 Is the secondary prompt string, when a shell needs more information to complete a command.
PS3 Is used for the select loop inside a script.
PS4 Is the execution trace prompt, for example when debugging a shell script.
Note: Generally you only want to customise the default PS1, many users just leave everything as defaults., However, some users go the other customise PS1 with a lot of useful information.
Some basic examples are:
Set PS1 to short hostname and username.
$ PS1="%m: %n> " saturn: trainer>
Set the PS1 to short hostname, username, and the current working directory.
$ PS1="%m: %n:%d> " saturn: trainer:/home/trainer> saturn: trainer:/home/trainer> cd /usr/local saturn: trainer:/usr/local>
Here's some of the options you can use, this is take from the zshmisc man page: https://www.lightnetics.com/post/6239, search for "SIMPLE PROMPT ESCAPES"
Login information %l The line (tty) the user is logged in on, without `/dev/' prefix. If the name starts with `/dev/tty', that prefix is stripped. %M The full machine hostname. %m The hostname up to the first `.'. An integer may follow the `%' to specify how many components of the hostname are desired. With a negative integer, trailing components of the hostname are shown. %n $USERNAME. %y The line (tty) the user is logged in on, without `/dev/' prefix. This does not treat `/dev/tty' names specially. Shell state %# A `#' if the shell is running with privileges, a `%' if not. Equivalent to `%(!.#.%%)'. The definition of `privileged', for these purposes, is that either the effective user ID is zero, or, if POSIX.1e capabilities are supported, that at least one capability is raised in either the Effective or Inheritable capability vectors. %? The return status of the last command executed just before the prompt. %_ The status of the parser, i.e. the shell constructs (like `if' and `for') that have been started on the command line. If given an integer number that many strings will be printed; zero or negative or no integer means print as many as there are. This is most useful in prompts PS2 for continuation lines and PS4 for debugging with the XTRACE option; in the latter case it will also work non-interactively. %^ The status of the parser in reverse. This is the same as `%_' other than the order of strings. It is often used in RPS2. %d %/ Current working directory. If an integer follows the `%', it specifies a number of trailing components of the current working directory to show; zero means the whole path. A negative inte‐ ger specifies leading components, i.e. %-1d specifies the first component. %~ As %d and %/, but if the current working directory starts with $HOME, that part is replaced by a `~'. Furthermore, if it has a named directory as its prefix, that part is replaced by a `~' followed by the name of the directory, but only if the result is shorter than the full path; see Dynamic and Static named direc‐ tories in zshexpn(1). %e Evaluation depth of the current sourced file, shell function, or eval. This is incremented or decremented every time the value of %N is set or reverted to a previous value, respectively. This is most useful for debugging as part of $PS4. %h %! Current history event number. %i The line number currently being executed in the script, sourced file, or shell function given by %N. This is most useful for debugging as part of $PS4. %I The line number currently being executed in the file %x. This is similar to %i, but the line number is always a line number in the file where the code was defined, even if the code is a shell function. %j The number of jobs. %L The current value of $SHLVL. %N The name of the script, sourced file, or shell function that zsh is currently executing, whichever was started most recently. If there is none, this is equivalent to the parameter $0. An inte‐ ger may follow the `%' to specify a number of trailing path com‐ ponents to show; zero means the full path. A negative integer specifies leading components. %x The name of the file containing the source code currently being executed. This behaves as %N except that function and eval com‐ mand names are not shown, instead the file where they were defined. %c %. %C Trailing component of the current working directory. An integer may follow the `%' to get more than one component. Unless `%C' is used, tilde contraction is performed first. These are depre‐ cated as %c and %C are equivalent to %1~ and %1/, respectively, while explicit positive integers have the same effect as for the latter two sequences.
© Lightnetics 2019