Linux/Environment Variables: Difference between revisions

From Wiki
(Created page with "== Basics == * List all environment variables <blockquote> <pre> env printenv </pre> </blockquote> * get variable <blockquote> <pre> echo $USER printenv USER </pre> </blockquote> * set variable <blockquote> <pre> VARIABLE='Hello world' echo $VARIABLE </pre> </blockquote> * promote a shell variable to an environment variable <blockquote> <pre> export VARIABLE='Hello world' </pre> </blockquote> * variable lists <blockquote> <pre> export VARIABLE='Hello world':'test'...")
 
mNo edit summary
Line 34: Line 34:
<blockquote>
<blockquote>
<pre>
<pre>
export VARIABLE='Hello world':'test'    # variable with two elements
VARIABLE='Hello world':'test'    # variable with two elements
 
VARIABLE=$VARIABLE:'test2'      # add another element
export VARIABLE=$VARIABLE:'test2'      # add another element
</pre>
</pre>
</blockquote>
</blockquote>
Line 48: Line 47:


== Use in scripts ==
== Use in scripts ==
 
* script.sh
<blockquote>
<pre>
DATE='01-01-2000'
export DATE
export VARIABLE="123"
</pre>
</blockquote>




[[Category:Linux/System]]
[[Category:Linux/System]]
[[Category:Linux]]
[[Category:Linux]]

Revision as of 11:29, 30 November 2023

Basics

  • List all environment variables
env
printenv
  • get variable
echo $USER
printenv USER
  • set variable
VARIABLE='Hello world'
echo $VARIABLE
  • promote a shell variable to an environment variable
export VARIABLE='Hello world'
  • variable lists
VARIABLE='Hello world':'test'    # variable with two elements
VARIABLE=$VARIABLE:'test2'       # add another element
  • remove variable
unset VARIABLE

Use in scripts

  • script.sh
DATE='01-01-2000'
export DATE
export VARIABLE="123"