Miscellaneous

  Home arrow Miscellaneous arrow Page 3 - Variables Within the UNIX Shell
MISCELLANEOUS

Variables Within the UNIX Shell
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2009-04-15

    Table of Contents:
  • Variables Within the UNIX Shell
  • Quotes in UNIX
  • The usage of curly braces
  • Environment Variables and the start of the shell
  • Language variables and some special cases

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Variables Within the UNIX Shell - The usage of curly braces


    (Page 3 of 5 )

     

    There are times when just putting the name of the variable inside a string delimited with the double quote is hard to make clear. Suppose I want to print "redHearth" on the terminal. More than this, I want to get the color of the hearth from a variable. I d not need the space between the two, and due to this, I will use the double quote to bypass the need to add this. 

    echo "$ColorHearth"

    Now the upper solution is what we can come up with; however, inside the quotes, the shell has a hard time figuring out if we wanted the variable "ColorHearth" or the color, and will probably choose the first option. To avoid situations like this, to make this clear and to introduce further opportunities, we use the {}. All we need to do is enclose inside these curly braces the name of the variables as follows:

    echo "${Color}Hearth"

    We can also make the following basic tests and replaces:

    ${color:-red}

    If the variable does not exist, it will return the string "red" and not assign a value to/create the variable.

    ${color:=red}

    Just like the earlier case above, however, now it will assign the string "red" to the variable and send it back after the string "red."

    ${color:?No color!}

    If the variable does not exist, the shell program / command line will stop with the given error message.

    More Miscellaneous Articles
    More By Gabor Bernat

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap