Variables

Command Line - Variables #

Assignment #

General form is variable=value.

$ foo="blah"
$ echo $foo
blah

Can also assign multiple things on a single line.

$ a="foo" b="bar"

Variable names #

  1. Must be alphanumeric or underscore

  2. First letter must be letter or underscore (no numbers)

  3. No spaces or punctuation

Conventions #

  • Upper letters for constants

  • Lower letters for true variables.

Expansion #

Variables can be nested.

$ a="something"
$ b="$a more" # "something more"
$ c="(ls -l foo.txt)" # shows the result of a command
$ d="((3 * 4))" # do math

Braces can also be used for expansion:

$ a="something"
$ echo ${a}1 #something1