Is there any difference between a shell variable and an environment variable? If so, what is the difference?
What command(s) can be used to display existing shell variables?
Note: This query is intended for RHCE-level and below!!!
Hi Trevor,
to display all the shell variables the command set needs to be used.
I would pipe the output of set into less so I would use : set | less to easily scroll through the list.
A shell variable is local to the current shell session and it cannot be accessed by other shells or processes, you will set it usually by my_var=value and of course without "export".
Shell variables are usually used to store temporary values or to restrict the scope only to the local shell session so it can not be shared outside of it .
An environment variable on the hand is accessible by the shell and the processes it spawns. In unix-like systems environment variables are typically capitalized by convention. to declare env variables or to make a shell variable an environment variable, you need to use the "export" command: export MY_VAR="Hello!"
PATH for example is a common environment variable.
when you set an environment variable in a terminal session, it will only last as long as that session remains open and to make it persist across sessions, you need to define it in shell config file like .bashrc if you use bash as your shell.
to display a shell variable you will use echo command :
echo $my_var
I like all of what you had to say Yassin! I'm holding off
on a thumbs up because of the command you gave
me, regarding a (single/one) variable.
I agree that I can display a (single) shell variable with the
command that you provided. However, referring back to
my initial post, I wanted to display existing shell variables
(plural, more than one). Got any ideas?
Hi Trevor,
to display all the shell variables the command set needs to be used.
I would pipe the output of set into less so I would use : set | less to easily scroll through the list.
You now have earned a thumbs up!!!
You've provided some great information yassin_ef!
Thank you for engaging!!!
Thanks! I'm glad to engage and contribute while continuing my learning journey to earn the RHCSA.
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.