⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 untyped.html

📁 Shall高级编程
💻 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&nbsp;#!/bin/bash   2&nbsp;# int-or-string.sh: Integer or string?   3&nbsp;   4&nbsp;a=2334                   # Integer.   5&nbsp;let "a += 1"   6&nbsp;echo "a = $a "           # a = 2335   7&nbsp;echo                     # Integer, still.   8&nbsp;   9&nbsp;  10&nbsp;b=${a/23/BB}             # Substitute "BB" for "23".  11&nbsp;                         # This transforms $b into a string.  12&nbsp;echo "b = $b"            # b = BB35  13&nbsp;declare -i b             # Declaring it an integer doesn't help.  14&nbsp;echo "b = $b"            # b = BB35  15&nbsp;  16&nbsp;let "b += 1"             # BB35 + 1 =  17&nbsp;echo "b = $b"            # b = 1  18&nbsp;echo  19&nbsp;  20&nbsp;c=BB34  21&nbsp;echo "c = $c"            # c = BB34  22&nbsp;d=${c/BB/23}             # Substitute "23" for "BB".  23&nbsp;                         # This makes $d an integer.  24&nbsp;echo "d = $d"            # d = 2334  25&nbsp;let "d += 1"             # 2334 + 1 =  26&nbsp;echo "d = $d"            # d = 2335  27&nbsp;echo  28&nbsp;  29&nbsp;# What about null variables?  30&nbsp;e=""  31&nbsp;echo "e = $e"            # e =  32&nbsp;let "e += 1"             # Arithmetic operations allowed on a null variable?  33&nbsp;echo "e = $e"            # e = 1  34&nbsp;echo                     # Null variable transformed into an integer.  35&nbsp;  36&nbsp;# What about undeclared variables?  37&nbsp;echo "f = $f"            # f =  38&nbsp;let "f += 1"             # Arithmetic operations allowed?  39&nbsp;echo "f = $f"            # f = 1  40&nbsp;echo                     # Undeclared variable transformed into an integer.  41&nbsp;  42&nbsp;  43&nbsp;  44&nbsp;# Variables in Bash are essentially untyped.  45&nbsp;  46&nbsp;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 + -