cancel
Showing results for 
Search instead for 
Did you mean: 
Trevor
Starfighter Starfighter
Starfighter
  • 179 Views

Shell Variable vs Environment Variable

Jump to solution

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!!!

Trevor "Red Hat Evangelist" Chandler
1 Solution

Accepted Solutions
yassin_ef
Cadet
Cadet
  • 104 Views

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.

View solution in original post

5 Replies
yassin_ef
Cadet
Cadet
  • 150 Views

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

Trevor
Starfighter Starfighter
Starfighter
  • 122 Views

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?

 

Trevor "Red Hat Evangelist" Chandler
yassin_ef
Cadet
Cadet
  • 105 Views

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.

Trevor
Starfighter Starfighter
Starfighter
  • 63 Views

You now have earned a thumbs up!!!

You've provided some great information yassin_ef!

Thank you for engaging!!!

Trevor "Red Hat Evangelist" Chandler
yassin_ef
Cadet
Cadet
  • 56 Views

Thanks! I'm glad to engage and contribute while continuing my learning journey to earn the RHCSA.

Join the discussion
You must log in to join this conversation.