Fetching values of environment variables


$ls
ascii-table     avg.txt               case-changer.c  exist.sh          mph-to-kph.c
ascii-table.c   avg-with-garbage.txt  env-var.c       functions_ver1.c  mph-to-kph_v2.c
ascii-table.md  case-changer          env-var-set.c   functions_ver2.c  output.c
$cat env-var.c 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
   /* Using getenv() to fetch env. variables */
   printf("Your username is %s\n", getenv("USER"));
   printf("Your home directory is %s\n", 
      getenv("HOME"));
   printf("Your preferred editor is %s\n", 
      getenv("EDITOR"));
   printf("Your shell is %s\n", getenv("SHELL"));

   /* Check if the current terminal support colors*/
   if ( strstr(getenv("TERM"), "256color")  )
   {
      /* Color the output with \033 + colorcode */
      printf("\033[0;31mYour \033[0;32mterminal "
         "\033[0;35msupport "
         "\033[0;33mcolors\033[0m\n");
   }
   else
   {
      printf("Your terminal doesn't support" 
         " colors\n");
   }
   return 0;
}
$gcc env-var.c -o env-var.c 
gcc: fatal error: input file ‘env-var.c’ is the same as output file
compilation terminated.
$gcc env-var.c -o env-var
$./env-var 
Your username is jeffrin
Your home directory is /home/jeffrin
Your preferred editor is (null)
Your shell is /bin/bash
Your terminal support colors
$

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: