The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to unset maxuprc in solarsi zones kalpeer SUN Solaris 1 06-15-2009 10:46 AM
How do I define a particular dir in PATH variable and then unset that dir Hangman2 Shell Programming and Scripting 2 01-06-2009 12:14 AM
how to unset the readonly variable Nidhi2177 UNIX for Advanced & Expert Users 2 09-12-2007 05:35 AM
How to unset 'finger' info? Chanakya.m Shell Programming and Scripting 3 01-23-2006 09:12 AM
Is there any way to set env variable in top level Makefile and unset when done umen Shell Programming and Scripting 0 10-23-2005 08:15 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-01-2009
balamv balamv is offline
Registered User
  
 

Join Date: May 2008
Posts: 24
How to unset all variables in shell?

can I use unset to unset all the variables in a shell sciprt?

VAR1=1
VAR2=2
VAR3=3
unset

whether this unset will afftect any system variables?

Thanks,
  #2 (permalink)  
Old 10-01-2009
ahmad.diab's Avatar
ahmad.diab ahmad.diab is offline
Registered User
  
 

Join Date: May 2008
Location: Amman Jordan in MEA
Posts: 228
Code:
The unset command deletes a shell variable,or non-shell variable effectively setting it to null. Note that this command
does not affect positional parameters

but note that you need to put the variable name after the unset command.

Example1:-
unset PATH
echo $PATH

Example2:-
variable=hello # Initialized.
echo "variable = $variable"
unset variable # Unset.
# Same effect as: variable=
echo "(unset) variable = $variable" # $variable is null.
BR
  #3 (permalink)  
Old 10-01-2009
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,420
The following function 'gsunset', deletes the variables specified by an RE :
Code:
gunset()
{
  local var_re="${1:-.*}"
  eval $( set | \
         awk -v FS='='        \
             -v VAR="$var_re" \
            '$1 ~ VAR { print "unset", $1 }' )
}

VAR1=value1
VAR2=value2
VAR3=value3

echo "Vars before gunset"
set | grep ^VAR
gunset 'VAR.*'
echo "Vars after gunset"
set | grep ^VAR
Output
Code:
Vars before gunset
VAR1=value1
VAR2=value2
VAR3=value3
Vars after gunset
Jean-Pierre.
  #4 (permalink)  
Old 10-01-2009
adderek adderek is offline
Registered User
  
 

Join Date: Sep 2007
Location: Poland
Posts: 111
Change your approach. NEVER use undeclared variables. There is an option "set -o nounset" if I remember correctly.
You should have something like this:

Code:
#!/bin/env ksh
typeset my_variable
typeset all_my_variables_should_be_defined_like_that
my_variable='abc'

Last edited by vgersh99; 10-01-2009 at 04:48 PM.. Reason: code tags, PLEASE!
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0