Friday, October 9, 2009

Linux - Bash Shell Scripting - Variables

Shell variables are used to store values. A variable can be defined at the same time it is assigned by the = operator, e.g.,

LONG_NAME="Flintstone Fred"
SHORTNAME=Fred

Note in the first case that quotation marks are required because the string contains spaces. The backquote operator will execute the enclosed command and assign the output to the designated variable, e.g.,

LS_OUT=`ls`

When referring to the variable, its name must be preceded by $, e.g.,

X1=$Y2

Here we are assigning the value of Y2 to X1. Note that Y2 is preceded by $ whereas X1 is not.

You can declare numerical values too,

intA=5

Summary 
  • Declare string

    • string1="string text"

  • Declare command

    • command1='ls'

  • Declare numerical value

    • intA=5
    • floatB=.5


No comments:

Post a Comment