Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
commands:builtin:unset [2012/11/15 14:20]
ormaaj Portability
commands:builtin:unset [2015/07/31 01:05] (current)
fgrose [Synopsis] f or v names separated by space
Line 4: Line 4:
  
 <​code>​ <​code>​
-unset [-f] [-v] [NAME...]+unset [-f|v] [-n] [NAME ...]
 </​code>​ </​code>​
  
Line 14: Line 14:
 ==== Options ==== ==== Options ====
  
-^Option ​ ^Description ​ +^ Option ​ ^ Description ​                                                                                                    ​
-|''​-f'' ​ |treats each ''​NAME''​ as a function name  +| ''​-f'' ​ | treats each ''​NAME''​ as a function name                                                                         ​
-|''​-v'' ​ |treats each ''​NAME''​ as a variable name  |+| ''​-v'' ​ | treats each ''​NAME''​ as a variable name                                                                         | 
 +| ''​-n'' ​ | treats each ''​NAME''​ as a name reference and unsets the variable itself rather than the variable it references ​ |
  
-==== Return ​status ====+==== Exit status ====
  
 ^Status ​ ^Reason ​ ^ ^Status ​ ^Reason ​ ^
Line 120: Line 121:
 </​code>​ </​code>​
  
-As usual in such cases, it'​s ​best to single-quote the args to avoid accidental results, or at least possibly horrible obfuscation.+As usual in such cases, it'​s ​important ​to quote the args to avoid accidental results ​such as globbing.
 <​code>​ <​code>​
- ~ $ ( a=({a..d}) b=a c=d d=1; set -x; unset "$b"'​['{2..3}'-c]'; declare -p a )+ ~ $ ( a=({a..d}) b=a c=d d=1; set -x; unset "${b}["{2..3}-c\]; declare -p a )
 + unset '​a[2-1]'​ '​a[3-1]'​ + unset '​a[2-1]'​ '​a[3-1]'​
 + declare -p a + declare -p a
Line 128: Line 129:
 </​code>​ </​code>​
 Of course hard to follow indirection is still possible whenever arithmetic is involved, also as shown above, even without extra expansions. Of course hard to follow indirection is still possible whenever arithmetic is involved, also as shown above, even without extra expansions.
 +
 +In Bash, the ''​unset''​ builtin only evaluates array subscripts if the array itself is set.
 +
 +<​code>​
 + ~ $ ( unset -v '​a[$(echo a was set >&​2)0]'​ )
 + ~ $ ( a=(); unset -v '​a[$(echo a was set >&​2)0]'​ )
 +a was set
 +</​code>​
  
 ===== Portability considerations ===== ===== Portability considerations =====