Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

10.6 Assignments

When setting several variables in a row, be aware that the order of the evaluation is undefined. For instance ‘foo=1 foo=2; echo $foo’ gives ‘1’ with Solaris /bin/sh, but ‘2’ with Bash. You must use ‘;’ to enforce the order: ‘foo=1; foo=2; echo $foo’.

Don't rely on the following to find subdir/program:

     PATH=subdir$PATH_SEPARATOR$PATH program

as this does not work with Zsh 3.0.6. Use something like this instead:

     (PATH=subdir$PATH_SEPARATOR$PATH; export PATH; exec program)

Don't rely on the exit status of an assignment: Ash 0.2 does not change the status and propagates that of the last statement:

     $ false || foo=bar; echo $?
     1
     $ false || foo=`:`; echo $?
     0

and to make things even worse, QNX 4.25 just sets the exit status to 0 in any case:

     $ foo=`exit 1`; echo $?
     0

To assign default values, follow this algorithm:

  1. If the default value is a literal and does not contain any closing brace, use:
              : ${var='my literal'}
         
  2. If the default value contains no closing brace, has to be expanded, and the variable being initialized is not intended to be IFS-split (i.e., it's not a list), then use:
              : ${var="$default"}
         
  3. If the default value contains no closing brace, has to be expanded, and the variable being initialized is intended to be IFS-split (i.e., it's a list), then use:
              var=${var="$default"}
         
  4. If the default value contains a closing brace, then use:
              test "${var+set}" = set || var="has a '}'"
         

In most cases ‘var=${var="$default"}’ is fine, but in case of doubt, just use the last form. See Shell Substitutions, items ‘${var:-value}’ and ‘${var=value}’ for the rationale.


 
 
  Published under the terms of the GNU General Public License Design by Interspire