{"id":6933,"date":"2018-09-27T19:41:42","date_gmt":"2018-09-27T16:41:42","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-printenv-command\/"},"modified":"2018-09-27T19:41:42","modified_gmt":"2018-09-27T16:41:42","slug":"linux-printenv-command-tutorial-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-printenv-command-tutorial-for-beginners-with-examples\/","title":{"rendered":"Linux printenv Command Tutorial for Beginners (with Examples)"},"content":{"rendered":"<p>Environment variables play a major role in Linux. Most programs, whether command line based or GUI based, deal with environment variables in one way or the other. So naturally, there are tools that let you access environment variables from the command line.<\/p>\n<p>One such tool is <strong>printenv<\/strong>. In this tutorial, we will discuss printenv using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-printenv-command\">Linux printenv command<\/h2>\n<p>The printenv command in Linux provides you with the ability to view all or a part of the environment. Following is its syntax:<\/p>\n<p class=\"command\">printenv [OPTION]&#8230; [VARIABLE]&#8230;<\/p>\n<p>And here&#8217;s how the tool&#8217;s man page defines it:<\/p>\n<pre>Print the values of the specified environment VARIABLE(s). If no VARIABLE is specified, print <br\/>name and value pairs for them all.<\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a better idea on how the printenv command works.<\/p>\n<h2 id=\"q-how-to-use-printenv\">Q1. How to use printenv?<\/h2>\n<p>Basic usage is pretty easy. All you have to do is to run printenv sans any arguments.<\/p>\n<p class=\"command\">printenv<\/p>\n<p>Following is the output produced on my system:<\/p>\n<pre>CLUTTER_IM_MODULE=xim<br\/>LS_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=00: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:*.zst=01;31:*.tzst=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:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=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:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:<br\/>LESSCLOSE=\/usr\/bin\/lesspipe %s %s<br\/>XDG_MENU_PREFIX=gnome-<br\/>LANG=en_IN<br\/>DISPLAY=:0<br\/>GNOME_SHELL_SESSION_MODE=ubuntu<br\/>COLORTERM=truecolor<br\/>USERNAME=himanshu<br\/>XDG_VTNR=2<br\/>SSH_AUTH_SOCK=\/run\/user\/1000\/keyring\/ssh<br\/>MANDATORY_PATH=\/usr\/share\/gconf\/ubuntu.mandatory.path<br\/>XDG_SESSION_ID=2<br\/>USER=himanshu<br\/>DESKTOP_SESSION=ubuntu<br\/>QT4_IM_MODULE=xim<br\/>TEXTDOMAINDIR=\/usr\/share\/locale\/<br\/>GNOME_TERMINAL_SCREEN=\/org\/gnome\/Terminal\/screen\/44c42800_9a68_408d_8539_7a0e414b7986<br\/>DEFAULTS_PATH=\/usr\/share\/gconf\/ubuntu.default.path<br\/>PWD=\/home\/himanshu<br\/>HOME=\/home\/himanshu<br\/>TEXTDOMAIN=im-config<br\/>SSH_AGENT_PID=1918<br\/>QT_ACCESSIBILITY=1<br\/>XDG_SESSION_TYPE=x11<br\/>XDG_DATA_DIRS=\/usr\/share\/ubuntu:\/usr\/local\/share:\/usr\/share:\/var\/lib\/snapd\/desktop<br\/>XDG_SESSION_DESKTOP=ubuntu<br\/>GJS_DEBUG_OUTPUT=stderr<br\/>GTK_MODULES=gail:atk-bridge<br\/>WINDOWPATH=2<br\/>TERM=xterm-256color<br\/>SHELL=\/bin\/bash<br\/>VTE_VERSION=5201<br\/>QT_IM_MODULE=xim<br\/><a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"b7effaf8f3fef1fef2e5e48af7deda\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a>=ibus<br\/>IM_CONFIG_PHASE=2<br\/>XDG_CURRENT_DESKTOP=ubuntu:GNOME<br\/>GPG_AGENT_INFO=\/run\/user\/1000\/gnupg\/S.gpg-agent:0:1<br\/>GNOME_TERMINAL_SERVICE=:1.61<br\/>XDG_SEAT=seat0<br\/>SHLVL=1<br\/>GDMSESSION=ubuntu<br\/>GNOME_DESKTOP_SESSION_ID=this-is-deprecated<br\/>LOGNAME=himanshu<br\/>DBUS_SESSION_BUS_ADDRESS=unix:path=\/run\/user\/1000\/bus<br\/>XDG_RUNTIME_DIR=\/run\/user\/1000<br\/>XAUTHORITY=\/run\/user\/1000\/gdm\/Xauthority<br\/>XDG_CONFIG_DIRS=\/etc\/xdg\/xdg-ubuntu:\/etc\/xdg<br\/>PATH=\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin:\/usr\/games:\/usr\/local\/games:\/snap\/bin<br\/>GJS_DEBUG_TOPICS=JS ERROR;JS LOG<br\/>SESSION_MANAGER=local\/himanshu:@\/tmp\/.ICE-unix\/1823,unix\/himanshu:\/tmp\/.ICE-unix\/1823<br\/>LESSOPEN=| \/usr\/bin\/lesspipe %s<br\/>GTK_IM_MODULE=ibus<br\/>_=\/usr\/bin\/printenv<\/pre>\n<p>So you can see that almost all environment variables set in my system were produced in output.<\/p>\n<h2 id=\"q-how-to-make-printenv-display-only-select-info\">Q2. How to make printenv display only select info?<\/h2>\n<p>Suppose you want printenv to display details related to only a specific environment variable in output, then all you have to do is the pass the name of that variable as input to the command.<\/p>\n<p>Here&#8217;s an example:<\/p>\n<p class=\"command\">printenv SHELL<\/p>\n<p>And here&#8217;s the output produced by this command:<\/p>\n<pre>\/bin\/bash<\/pre>\n<p>Of course, you can add more variables to the input list. For example, the following command:<\/p>\n<p class=\"command\">printenv SHELL HOME<\/p>\n<p>produced the output shown below:<\/p>\n<pre>\/bin\/bash<br\/>\/home\/himanshu<\/pre>\n<h2 id=\"q-how-to-make-printenv-use-nul-instead-of-newline\">Q3. How to make printenv use NUL instead of newline?<\/h2>\n<p>By default, the printenv command uses newline as the terminating character between output entries. However, if you want, you can make the tool use NUL as the terminator instead.<\/p>\n<p>For this, you&#8217;ll have to use the &#8211;null command line option. Following is an example:<\/p>\n<p class=\"command\">printenv &#8211;null SHELL HOME<\/p>\n<p>And here&#8217;s a screenshot of the output produced:<\/p>\n<p><a class=\"fancybox\" id=\"img-printenv-nul\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/printenv-nul.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-printenv-command-tutorial-for-beginners-with-examples.png\" alt=\"How to make printenv use NUL instead of newline\" width=\"430\" height=\"38\" title=\"\"><\/a><\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>If your Linux command line work involves dealing with environment variables, the printenv command is an important tool to remember. Here, in this tutorial, we have discussed majority of the options this tool offers. Once you are done practicing these (the learning curve isn&#8217;t steep, so it shouldn&#8217;t take time), you can head to the printenv\u00a0<a href=\"https:\/\/linux.die.net\/man\/1\/printenv\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> to learn more about this command line utility.<\/p>\n<div class=\"authorbox\" readability=\"15\">\n<img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-printenv-command-tutorial-for-beginners-with-examples.jpg\" alt=\"Himanshu Arora\" title=\"\"> <\/p>\n<p><strong>About Himanshu Arora<\/strong><\/p>\n<p>Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu&#8217;s work has also been featured in some of world&#8217;s other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.<\/p>\n<\/div>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-printenv-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-printenv-command-tutorial-for-beginners-with-examples-1.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-printenv-command%2F&amp;text=Linux+printenv+Command+Tutorial+for+Beginners+%28with+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-printenv-command-tutorial-for-beginners-with-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-printenv-command-tutorial-for-beginners-with-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-printenv-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/09\/linux-printenv-command-tutorial-for-beginners-with-examples-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Environment variables play a major role in Linux. Most programs, whether command line based or GUI based, deal with environment variables in one way or the other. So naturally, there are tools that let you access environment variables from the command line. One such tool is printenv. In this tutorial, we will discuss printenv using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-6933","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/6933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=6933"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/6933\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=6933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=6933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=6933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}