RSS Subscription
Linux Howtos & Tutorials

Enter your email:

Delivered by


NOTE:New tutorials are from LinuxCareer.com

Poll

Do you own or wish to have iPhone?
 


Linux eBooks FREE Download
A guide to programming Linux kernel modules
Introduction to Linux - A Hands on Guide
A Newbie's Getting Started Guide to Linux

Linux from Scratch - Create Your Own Linux System - Free eBook

Linux: The Hacking Solution (v.3.0)

SQLite 3 with PHP Essential Training – Free Video Training Tutorials

This guide will introduce you to the world of GNU/Linux

The GNU/Linux Advanced Administration

A Complete Beginner's Manual for Ubuntu 10.04 (Lucid Lynx)

Advanced Bash-Scripting Guide

Set up, maintain, and secure a small office email server

Partner Linux Sites:
How-To.LinuxCareer.com
Jobs.LinuxCareer.com
TuxMachines
Monsterb
LinuxBloggers
AdamsInfo
LinuxScrew
All For Linux

Set and Get environmental shell variable using c++

Here is a small example on how to set and get environmental variables using getnenv() and putenv() functions defined by C/C++ stdlib.h library. Environmental variable expansion is a great feature of a Linux shell as it enables programmers and users to rely on the environment settings of each user separately. C++ getenv() will read all exported environmental variables and putenv() will set existing or create new variables. Here is a small c++ program which can do this job:

 
#include <stdlib.h> 
#include <iostream> 
 
int main() { 
 
// get and print shell environmental variable home 
std::cout << "SHELL = " << getenv("SHELL") << std::endl; 
std::cout << "MYENV = " << getenv("MYENV") << std::endl; 
 
//set new shell environmental variable using putenv 
char mypath[]="TEMP=/my/new/temp/path/"; 
putenv( mypath ); 
 
std::cout << "TEMP = " << getenv("TEMP") << std::endl; 
 
return 0; 
 
}

Now lets' try to export new shell environment variable MYENV:

$ export MYENV=linuxconfig.org

Compile c++ program:

$ g++ shell_env.cpp -o shell_env

Run:

$ ./shell_env

Output:

SHELL = /bin/bash
MYENV = linuxconfig.org
TEMP = /my/new/temp/path/

Share this linux post:

Submit Set and Get environmental shell variable using c++ in Delicious Submit Set and Get environmental shell variable using c++ in Digg Submit Set and Get environmental shell variable using c++ in FaceBook Submit Set and Get environmental shell variable using c++ in Google Bookmarks Submit Set and Get environmental shell variable using c++ in Stumbleupon Submit Set and Get environmental shell variable using c++ in Technorati Submit Set and Get environmental shell variable using c++ in Twitter
 
Comments for this page are closed !!!
Please visit our new Linux Forum for additional help or discussion.


Linux eBooks FREE Download