{"id":2071,"date":"2021-01-26T09:41:37","date_gmt":"2021-01-26T09:41:37","guid":{"rendered":"https:\/\/swiv.com.br\/linux-environment-variables\/"},"modified":"2026-05-27T20:02:53","modified_gmt":"2026-05-27T19:02:53","slug":"linux-environment-variables","status":"publish","type":"post","link":"https:\/\/swiv.com.br\/index.php\/2021\/01\/26\/linux-environment-variables\/","title":{"rendered":"Linux Environment Variables"},"content":{"rendered":"\n<p>Assim que carregamos o shell, uma s\u00e9rie de vari\u00e1veis de ambientes s\u00e3o carregadas, para serem utilizadas pelos processos do shell, pelos comandos, interfaces gr\u00e1ficas, etc. <\/p>\n\n\n\n<p>Para definir manualmente o valor de uma vari\u00e1vel, basta definir o seu nome e conte\u00fado como no exemplo abaixo:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# VARIAVEL=BSS\n&#x5B;root@oel7 ~]# echo VARIAVEL\nBSS\n<\/pre><\/div>\n\n\n<p>Por\u00e9m, essa vari\u00e1vel acima est\u00e1 declarada localmente, ou seja, s\u00f3 \u00e9 vis\u00edvel nesta sess\u00e3o. No shellscript abaixo eu tento imprimir o seu valor, mas como em tempo de execu\u00e7\u00e3o \u00e9 aberto um outro processo no bash, este novo processo n\u00e3o \u00e9 capaz de ler o conte\u00fado da vari\u00e1vel (pois ela \u00e9 local):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# cat variavel.sh\necho &quot;O valor da VARIAVEL \u00e9:&quot; $VARIAVEL\n&#x5B;root@oel7 ~]# .\/variavel.sh\nO valor da VARIAVEL \u00e9:\n&#x5B;root@oel7 ~]# echo VARIAVEL\nBSS\n&#x5B;root@oel7 ~]#\n<\/pre><\/div>\n\n\n<p>Para que outros processos tenham a capacidade de ler o valor desta vari\u00e1vel, podemos export\u00e1-la, conforme exemplo abaixo (que demonstra uma vari\u00e1vel global):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# export VARIAVEL\n&#x5B;root@oel7 ~]# .\/variavel.sh\nO valor da VARIAVEL \u00e9: BSS\n&#x5B;root@oel7 ~]#\n<\/pre><\/div>\n\n\n<p>Na situa\u00e7\u00e3o abaixo, criei uma nova vari\u00e1vel local, digitei o comando bash (que abre um novo processo vinculado com o processo pai) e consigo reparar que ele n\u00e3o consegue ler o valor:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# VARIAL2=BSS\n&#x5B;root@oel7 ~]# bash\n&#x5B;root@oel7 ~]# echo VARIAVEL2\n \n&#x5B;root@oel7 ~]#\n<\/pre><\/div>\n\n\n<p>Tornando a vari\u00e1vel global e testando novamente:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# export VARIAL2\n&#x5B;root@oel7 ~]# bash\n&#x5B;root@oel7 ~]# echo VARIAL2\nBSS\n&#x5B;root@oel7 ~]#\n<\/pre><\/div>\n\n\n<p>Por\u00e9m, se abrirmos uma nossa sess\u00e3o e tentarmos ver o valor das vari\u00e1veis, n\u00e3o conseguiremos, pois a forma que foram criadas e exportadas s\u00f3 permitem a sua visualiza\u00e7\u00e3o de processos bash filhas do processo original:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# .\/variavel.sh\nO valor da VARIAVEL \u00e9:\n&#x5B;root@oel7 ~]#\n<\/pre><\/div>\n\n\n<p>O comando SET mostrar\u00e1 todas as vari\u00e1veis locais e exportadas:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# set | grep VARIAVEL; set | grep VARIAL2\nVARIAVEL=BSS\nVARIAL2=BSS\n&#x5B;root@oel7 ~]#\n&#x5B;root@oel7 ~]# set\nABRT_DEBUG_LOG=\/dev\/null\nBASH=\/usr\/bin\/bash\nBASHOPTS=checkwinsize:cmdhist:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath\nBASH_ALIASES=()\nBASH_ARGC=()\nBASH_ARGV=()\nBASH_CMDS=()\nBASH_COMPLETION_COMPAT_DIR=\/etc\/bash_completion.d\nBASH_LINENO=()\nBASH_SOURCE=()\nBASH_VERSINFO=(&#x5B;0]=&quot;4&quot; &#x5B;1]=&quot;2&quot; &#x5B;2]=&quot;46&quot; &#x5B;3]=&quot;2&quot; &#x5B;4]=&quot;release&quot; &#x5B;5]=&quot;x86_64-redhat-linux-gnu&quot;)\nBASH_VERSION=&#039;4.2.46(2)-release&#039;\n&#x5B;...]\n<\/pre><\/div>\n\n\n<p>J\u00e1 o comando ENV mostra apenas as vari\u00e1veis que s\u00e3o globais:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# env\nXDG_SESSION_ID=1\nHOSTNAME=oel7.localdomain\nSELINUX_ROLE_REQUESTED=\nSHELL=\/bin\/bash\nTERM=xterm\nHISTSIZE=1000\nSSH_CLIENT=192.168.0.100 52860 22\nSELINUX_USE_CURRENT_RANGE=\nSSH_TTY=\/dev\/pts\/0\nUSER=root\nLS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:\nVARIAL2=BSS\nPATH=\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/root\/bin\nMAIL=\/var\/spool\/mail\/root\nPWD=\/root\nLANG=en_US.UTF-8\nSELINUX_LEVEL_REQUESTED=\nVARIAVEL=BSS\nHISTCONTROL=ignoredups\nHOME=\/root\nSHLVL=2\nLOGNAME=root\nSSH_CONNECTION=192.168.0.100 52860 192.168.0.109 22\nXDG_DATA_DIRS=\/root\/.local\/share\/flatpak\/exports\/share:\/var\/lib\/flatpak\/exports\/share:\/usr\/local\/share:\/usr\/share\nLESSOPEN=||\/usr\/bin\/lesspipe.sh %s\nDISPLAY=localhost:10.0\nXDG_RUNTIME_DIR=\/run\/user\/0\n_=\/usr\/bin\/env\n&#x5B;root@oel7 ~]#\n<\/pre><\/div>\n\n\n<p>Essas diferen\u00e7as entre o SET e ENV s\u00e3o claras uma vez que um \u00e9 um programa interno bash, e o outro \u00e9 um programa externo:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# type set\nset is a shell builtin\n&#x5B;root@oel7 ~]# type env\nenv is hashed (\/usr\/bin\/env)\n<\/pre><\/div>\n\n\n<p>Outra funcionalidade do comando ENV \u00e9 nos permitir setar o valor de uma vari\u00e1vel em tempo de execu\u00e7\u00e3o:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# env VARIAVEL=BRASIL .\/variavel.sh\nO valor da VARIAVEL \u00e9: BRASIL\n&#x5B;root@oel7 ~]# echo VARIAVEL\nBSS\n<\/pre><\/div>\n\n\n<p>Para remover uma defini\u00e7\u00e3o de vari\u00e1vel, basta usar o comando UNSET:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# unset VARIAVEL\n&#x5B;root@oel7 ~]# echo $VARIAVEL\n<\/pre><\/div>\n\n\n<p>Existem vari\u00e1veis de ambiente que s\u00e3o definidas dinamicamente pelo shell, e s\u00e3o identificadas pelo $ como prefixo. Por exemplo, para mostrar o PID corrente do shell (o ID do processo):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# echo $$\n32261\n<\/pre><\/div>\n\n\n<p>Para visualizar o PID do \u00faltimo bash executado:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# top &amp;amp;\n&#x5B;2] 488\n&#x5B;root@oel7 ~]#\n \n&#x5B;2]+  Stopped                 top\n&#x5B;root@oel7 ~]#\n&#x5B;root@oel7 ~]# echo $!\n488\n&#x5B;root@oel7 ~]#\n<\/pre><\/div>\n\n\n<p>Para visualizar o c\u00f3digo de retorno de um comando (o c\u00f3digo 0 significa que o comando executou com sucesso):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# cat variavel.sh\necho &quot;O valor da VARIAVEL \u00e9:&quot; $VARIAVEL\n&#x5B;root@oel7 ~]# echo $?\n0\n<\/pre><\/div>\n\n\n<p>Para visualizar o home do usu\u00e1rio atual:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# echo ~\n\/root\n<\/pre><\/div>\n\n\n<p>Se incluirmos o nome do usu\u00e1rio ap\u00f3s o ~, temos o home do mesmo:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \\&quot;wp-block-syntaxhighlighter-code\\&quot;\"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@oel7 ~]# echo ~teste\n\/home\/teste\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Assim que carregamos o shell, uma s\u00e9rie de vari\u00e1veis de ambientes s\u00e3o carregadas, para serem utilizadas pelos processos do shell, pelos comandos, interfaces gr\u00e1ficas, etc. Para definir manualmente o valor de uma vari\u00e1vel, basta definir o seu nome e conte\u00fado como no exemplo abaixo: Por\u00e9m, essa vari\u00e1vel acima est\u00e1 declarada localmente, ou seja, s\u00f3 \u00e9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[21],"class_list":["post-2071","post","type-post","status-publish","format-standard","hentry","category-linux","tag-oracle-linux"],"_links":{"self":[{"href":"https:\/\/swiv.com.br\/index.php\/wp-json\/wp\/v2\/posts\/2071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swiv.com.br\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swiv.com.br\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swiv.com.br\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swiv.com.br\/index.php\/wp-json\/wp\/v2\/comments?post=2071"}],"version-history":[{"count":1,"href":"https:\/\/swiv.com.br\/index.php\/wp-json\/wp\/v2\/posts\/2071\/revisions"}],"predecessor-version":[{"id":9267,"href":"https:\/\/swiv.com.br\/index.php\/wp-json\/wp\/v2\/posts\/2071\/revisions\/9267"}],"wp:attachment":[{"href":"https:\/\/swiv.com.br\/index.php\/wp-json\/wp\/v2\/media?parent=2071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swiv.com.br\/index.php\/wp-json\/wp\/v2\/categories?post=2071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swiv.com.br\/index.php\/wp-json\/wp\/v2\/tags?post=2071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}