📄 varassignment.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Variable Assignment</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Introduction to Variables and Parameters"HREF="variables.html"><LINKREL="PREVIOUS"TITLE="Introduction to Variables and Parameters"HREF="variables.html"><LINKREL="NEXT"TITLE="Bash Variables Are Untyped"HREF="untyped.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="variables.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 4. Introduction to Variables and Parameters</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="untyped.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="VARASSIGNMENT"></A>4.2. Variable Assignment</H1><DIVCLASS="VARIABLELIST"><DL><DT><ANAME="EQREF"></A><SPANCLASS="TOKEN">=</SPAN></DT><DD><P>the assignment operator (<SPANCLASS="emphasis"><ICLASS="EMPHASIS">no space before and after</I></SPAN>)</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Do not confuse this with <AHREF="comparison-ops.html#EQUALSIGNREF">=</A> and <AHREF="comparison-ops.html#EQUALREF">-eq</A>, which test, rather than assign!</P><P>Note that <SPANCLASS="TOKEN">=</SPAN> can be either an assignment or a test operator, depending on context.</P></TD></TR></TABLE></DIV><P><ANAME="EX15_0"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="EX15"></A><P><B>Example 4-2. Plain Variable Assignment</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # Naked variables 3 4 echo 5 6 # When is a variable "naked", i.e., lacking the '$' in front? 7 # When it is being assigned, rather than referenced. 8 9 # Assignment 10 a=879 11 echo "The value of \"a\" is $a." 12 13 # Assignment using 'let' 14 let a=16+5 15 echo "The value of \"a\" is now $a." 16 17 echo 18 19 # In a 'for' loop (really, a type of disguised assignment): 20 echo -n "Values of \"a\" in the loop are: " 21 for a in 7 8 9 11 22 do 23 echo -n "$a " 24 done 25 26 echo 27 echo 28 29 # In a 'read' statement (also a type of assignment): 30 echo -n "Enter \"a\" " 31 read a 32 echo "The value of \"a\" is now $a." 33 34 echo 35 36 exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="EX16_0"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="EX16"></A><P><B>Example 4-3. Variable Assignment, plain and fancy</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 a=23 # Simple case 4 echo $a 5 b=$a 6 echo $b 7 8 # Now, getting a little bit fancier (command substitution). 9 10 a=`echo Hello!` # Assigns result of 'echo' command to 'a' 11 echo $a 12 # Note that including an exclamation mark (!) within a 13 #+ command substitution construct #+ will not work from the command line, 14 #+ since this triggers the Bash "history mechanism." 15 # Inside a script, however, the history functions are disabled. 16 17 a=`ls -l` # Assigns result of 'ls -l' command to 'a' 18 echo $a # Unquoted, however, removes tabs and newlines. 19 echo 20 echo "$a" # The quoted variable preserves whitespace. 21 # (See the chapter on "Quoting.") 22 23 exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="COMMANDSUBREF0"></A></P><P>Variable assignment using the <ICLASS="FIRSTTERM">$(...)</I> mechanism (a newer method than <AHREF="commandsub.html#BACKQUOTESREF">backquotes</A>). This is actually a form of <AHREF="commandsub.html#COMMANDSUBREF">command substitution</A>.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 # From /etc/rc.d/rc.local 2 R=$(cat /etc/redhat-release) 3 arch=$(uname -m)</PRE></TD></TR></TABLE></P></DD></DL></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="variables.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="untyped.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Introduction to Variables and Parameters</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="variables.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Bash Variables Are Untyped</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -