When we print the value of PS1
, it is set:
$ echo $PS1
[\u@\h \W]\$
We can use env
command to print environment variables. Why does it not list PS1
variable ?
$ env | grep PS1
# No output here
Because PS1 is not (usually, and in your specific case) an environment variable.
There are many variables set in a bash instance which you can list with:
$ set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:…
BASH_ALIASES=()
BASH_ARGC=()
.
.
SHLVL=1
SSH_AGENT_PID=853
SSH_AUTH_SOCK=/tmp/ssh-Ofupc03xWIs7/agent.795
TERM=xterm-256color
But many of them are not environment variables. For example:
$ echo "$PPID"
1062
$ env | grep PPID
You can add variables to the environment by using export
. So PS1 could be set as an environment variable:
$ export PS1
$ env | grep PS1
PS1=\u@\h:\w\$
And a variable could be un-exported by removing its export flag with declare (which will retain the value of the variable, just not exported):
$ declare +x PS1
$ env | grep PS1
$ echo $PS1
\u@\h:\w\$
Or, more drastically, by unset
ing the variable:
$ unset PS1
$ env | grep PS1
In bash, declare
could be used to print the flags of variables:
$ declare -p PS1
declare -- PS1="\${debian_chroot:+(\$debian_chroot)}\\u@\\h:\\w\\\$ "
$ export PS1
$ declare -p PS1
declare -x PS1="\${debian_chroot:+(\$debian_chroot)}\\u@\\h:\\w\\\$ "
Note the -x
set for the variable after it is exported.
Depending on where the variable PS1 is set it can be considered local or environmental (see this post to set environmental).
If it's local then you cannot print it with env . But you can print it with the command set .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.