12.07.2015 Views

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4.2. Variable Assignment<strong>Advanced</strong> <strong>Bash</strong>-<strong>Scripting</strong> <strong>Guide</strong>=the assignment operator (no space before and after)Do not confuse this with = and -eq, which test, rather than assign!Note that = can be either an assignment or a test operator, depending on context.Example 4-2. Plain Variable Assignment#!/bin/bash# Naked variablesecho# When is a variable "naked", i.e., lacking the '$' in front?# When it is being assigned, rather than referenced.# Assignmenta=879echo "The value of \"a\" is $a."# Assignment using 'let'let a=16+5echo "The value of \"a\" is now $a."echo# In a 'for' loop (really, a type of disguised assignment):echo -n "Values of \"a\" in the loop are: "for a in 7 8 9 11doecho -n "$a "doneechoecho# In a 'read' statement (also a type of assignment):echo -n "Enter \"a\" "read aecho "The value of \"a\" is now $a."echoexit 0Example 4-3. Variable Assignment, plain and fancy#!/bin/basha=23 # Simple caseecho $ab=$aChapter 4. Introduction to Variables and Parameters 30

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!