0% found this document useful (0 votes)
40 views3 pages

4.2. Variable Assignment: The Assignment Operator (No Space Before and After)

This document discusses variable assignment in Bash scripting. It explains that the = operator is used for assignment and should not be confused with comparison operators. It provides examples of assigning values to variables using plain variable assignment, the let command, loops, read statements, and command substitution. Command substitution allows assigning the output of a command to a variable. Variable assignment using $(...) is also covered as a newer method than backquotes.

Uploaded by

Mehul Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

4.2. Variable Assignment: The Assignment Operator (No Space Before and After)

This document discusses variable assignment in Bash scripting. It explains that the = operator is used for assignment and should not be confused with comparison operators. It provides examples of assigning values to variables using plain variable assignment, the let command, loops, read statements, and command substitution. Command substitution allows assigning the output of a command to a variable. Variable assignment using $(...) is also covered as a newer method than backquotes.

Uploaded by

Mehul Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Advanced Bash-Scripting Guide:

Prev Next
([Link]) Chapter 4. Introduction to Variables and Parameters ([Link])

4.2. Variable Assignment


=
the assignment operator (no space before and after)
Do not confuse this with = ([Link]#EQUALSIGNREF) and -eq
([Link]#EQUALREF), which test ([Link]#IFTHEN), 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 variables

echo

# When is a variable "naked", i.e., lacking the '$' in front?


# When it is being assigned, rather than referenced.

# Assignment
a=879
echo "The value of \"a\" is $a."

# Assignment using 'let'


let a=16+5
echo "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 11
do
echo -n "$a "
done

echo
echo

# In a 'read' statement (also a type of assignment):


echo -n "Enter \"a\" "
read a
echo "The value of \"a\" is now $a."

echo

exit 0

Example 4-3. Variable Assignment, plain and fancy


#!/bin/bash

a=23 # Simple case


echo $a
b=$a
echo $b

# Now, getting a little bit fancier (command substitution).

a=`echo Hello!` # Assigns result of 'echo' command to 'a' ...


echo $a
# Note that including an exclamation mark (!) within a
#+ command substitution construct will not work from the command-line,
#+ since this triggers the Bash "history mechanism."
# Inside a script, however, the history functions are disabled by default.

a=`ls -l` # Assigns result of 'ls -l' command to 'a'


echo $a # Unquoted, however, it removes tabs and newlines.
echo
echo "$a" # The quoted variable preserves whitespace.
# (See the chapter on "Quoting.")

exit 0

Variable assignment using the $(...) mechanism (a newer method than backquotes
([Link]#BACKQUOTESREF)). This is likewise a form of command
substitution ([Link]#COMMANDSUBREF).

# From /etc/rc.d/[Link]
R=$(cat /etc/redhat-release)
arch=$(uname -m)

Prev ([Link]) Home ([Link]) Next ([Link])


Variable Substitution Up ([Link]) Bash Variables Are Untyped

You might also like