Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why env does not print PS1 variable?

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
like image 314
tuxdna Avatar asked Jan 30 '17 08:01

tuxdna


People also ask

Is PS1 an environment variable?

The PS1 variable contains the value of the default prompt. It is used to change the looks and environment of the shell command prompt. Different examples of using the PS1 variable have been shown in this tutorial.

Can I use variables in .env file?

You can set default values for environment variables using a .env file, which Compose automatically looks for in project directory (parent folder of your Compose file). Values set in the shell environment override those set in the .env file.

How do I print the contents of environment variables in Linux?

To display the values of environment variables, use the printenv command. If you specify the Name parameter, the system only prints the value associated with the variable you requested.


2 Answers

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 unseting 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.

like image 181
IsaaC Avatar answered Oct 10 '22 22:10

IsaaC


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.

like image 34
terence hill Avatar answered Oct 10 '22 23:10

terence hill