📄 untyped.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Bash Variables Are Untyped</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="Variable Assignment"HREF="varassignment.html"><LINKREL="NEXT"TITLE="Special Variable Types"HREF="othertypesv.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="varassignment.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="othertypesv.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="UNTYPED"></A>4.3. Bash Variables Are Untyped</H1><P><ANAME="BVUNTYPED"></A></P><P>Unlike many other programming languages, Bash does not segregate its variables by <SPANCLASS="QUOTE">"type."</SPAN> Essentially, Bash variables are character strings, but, depending on context, Bash permits integer operations and comparisons on variables. The determining factor is whether the value of a variable contains only digits.</P><DIVCLASS="EXAMPLE"><HR><ANAME="INTORSTRING"></A><P><B>Example 4-4. Integer or string?</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # int-or-string.sh: Integer or string? 3 4 a=2334 # Integer. 5 let "a += 1" 6 echo "a = $a " # a = 2335 7 echo # Integer, still. 8 9 10 b=${a/23/BB} # Substitute "BB" for "23". 11 # This transforms $b into a string. 12 echo "b = $b" # b = BB35 13 declare -i b # Declaring it an integer doesn't help. 14 echo "b = $b" # b = BB35 15 16 let "b += 1" # BB35 + 1 = 17 echo "b = $b" # b = 1 18 echo 19 20 c=BB34 21 echo "c = $c" # c = BB34 22 d=${c/BB/23} # Substitute "23" for "BB". 23 # This makes $d an integer. 24 echo "d = $d" # d = 2334 25 let "d += 1" # 2334 + 1 = 26 echo "d = $d" # d = 2335 27 echo 28 29 # What about null variables? 30 e="" 31 echo "e = $e" # e = 32 let "e += 1" # Arithmetic operations allowed on a null variable? 33 echo "e = $e" # e = 1 34 echo # Null variable transformed into an integer. 35 36 # What about undeclared variables? 37 echo "f = $f" # f = 38 let "f += 1" # Arithmetic operations allowed? 39 echo "f = $f" # f = 1 40 echo # Undeclared variable transformed into an integer. 41 42 43 44 # Variables in Bash are essentially untyped. 45 46 exit 0</PRE></TD></TR></TABLE><HR></DIV><P>Untyped variables are both a blessing and a curse. They permit more flexibility in scripting (enough rope to hang yourself!) and make it easier to grind out lines of code. However, they permit errors to creep in and encourage sloppy programming habits.</P><P>To lighten the burden of keeping track of variable types in a script, Bash <SPANCLASS="emphasis"><ICLASS="EMPHASIS">does</I></SPAN> permit <AHREF="declareref.html">declaring</A> variables.</P></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="varassignment.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="othertypesv.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Variable Assignment</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="variables.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Special Variable Types</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -