13-B.5: Strings / Arrays / Functions
- Page ID
- 43192
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Linux String Literals
A lot of keys have special meanings in some context or other. Quoting is used to remove the special meaning of characters or words. Quotes can disable special treatment for special characters, they can prevent reserved words from being recognized as such and they can disable parameter expansion.
Escape Characters
Escape characters are used to remove the special meaning from a single character. A non-quoted backslash, \, is used as an escape character in Bash. It preserves the literal value of the next character that follows, with the exception of newline. If a newline character appears immediately after the backslash, it marks the continuation of a line when it is longer than the width of the terminal; the backslash is removed from the input stream and effectively ignored.
pbmac@pbmac-server $ date=20021226
pbmac@pbmac-server $ echo $date
20021226
pbmac@pbmac-server $ echo \$date
$date
In this example, the variable date is created and set to hold a value. The first echo displays the value of the variable, but for the second, the dollar sign is escaped.
Single Quotes
Single quotes ('') are used to preserve the literal value of each character enclosed within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.
We continue with the previous example:
pbmac@pbmac-server $ echo '$date'
$date
Double Quotes
Using double quotes the literal value of all characters enclosed is preserved, except for the dollar sign, the backticks (backward single quotes, ``) and the backslash.
The dollar sign and the backticks retain their special meaning within the double quotes.
The backslash retains its meaning only when followed by dollar, backtick, double quote, backslash or newline. Within double quotes, the backslashes are removed from the input stream when followed by one of these characters. Backslashes preceding characters that don't have a special meaning are left unmodified for processing by the shell interpreter.
A double quote may be quoted within double quotes by preceding it with a backslash.
pbmac@pbmac-server $ echo "$date"
20021226
Notice the back quote (its the key to the left of the number 1 key) - this runs the command - so the command date is run and its
displayed
pbmac@pbmac-server $ echo "`date`"
Sun Apr 20 11:22:06 CEST 2003
pbmac@pbmac-server $ echo "I'd say: \"Go for it!\""
I'd say: "Go for it!"
pbmac@pbmac-server $ echo "\"
More input>"
pbmac@pbmac-server $ echo "\\"
\
Linux Shell Arrays
An array is a variable containing multiple values. Any variable may be used as an array. There is no maximum limit to the size of an array, nor any requirement that member variables be indexed or assigned contiguously. Arrays are zero-based: the first element is indexed with the number 0.
Indirect declaration is done using the following syntax to declare a variable:
ARRAY[INDEXNR]=value
The INDEXNR is treated as an arithmetic expression that must evaluate to a positive number.
Explicit declaration of an array is done using the declare built-in:
declare -a ARRAYNAME
A declaration with an index number will also be accepted, but the index number will be ignored. Attributes to the array may be specified using the declare and readonly built-ins. Attributes apply to all variables in the array; you can't have mixed arrays.
Array variables may also be created using compound assignments in this format:
ARRAY=(value1 value2 ... valueN)
Each value is then in the form of [indexnumber]=string. The index number is optional. If it is supplied, that index is assigned to it; otherwise the index of the element assigned is the number of the last index that was assigned, plus one. This format is accepted by declare as well. If no index numbers are supplied, indexing starts at zero.
Adding missing or extra members in an array is done using the syntax:
ARRAYNAME[indexnumber]=value
Remember that the read built-in provides the -a option, which allows for reading and assigning values for member variables of an array.
Dereferencing the Variables in an Array
In order to refer to the content of an item in an array, use curly braces. This is necessary, as you can see from the following example, to bypass the shell interpretation of expansion operators. If the index number is @ or *, all members of an array are referenced.
Declare an array with 3 elements
pbmac@pbmac-server $ ARRAY=(one two three)
NOTICE - the curly brackets around the array name
pbmac@pbmac-server $ echo ${ARRAY[*]}
one two three
The brackets are not present - this changes how the shell interprets the value
pbmac@pbmac-server $ echo $ARRAY[*]
one[*]
both the brackets and the index are supplied.
pbmac@pbmac-server $ echo ${ARRAY[2]}
three
add one more value, then print out the entire array
pbmac@pbmac-server $ ARRAY[3]=four
pbmac@pbmac-server $ echo ${ARRAY[*]}
one two three four
Linux Shell Functions
As in almost any programming language, you can use functions to group pieces of code in a more logical way or practice the divine art of recursion.
Declaring a function is just a matter of writing function my_func { my_code }.
Calling a function is just like calling another program, you just write its name.
Example - call this script hello.sh:
#!/bin/bash function quit { exit } function hello { echo Hello! } # The next line calls the hello function hello # The next line calls the quit function quit # The next line simply echos foo echo foo
Notice that functions don't need to be declared in any specific order.
Lines 2-4 contain the 'quit' function. Lines 5-7 contain the 'hello' function. If you are not absolutely sure about what this script does, please try it!
When running the script you'll notice that first, the function 'hello' is called, second the 'quit' function, and the program never reaches line 10.
Functions with Parameters Sample
#!/bin/bash
function quit {
exit
}
function e {
echo $1
}
e Hello
e World
quit
This script is almost identical to the previous one. The main difference is the function 'e.' This function prints the first argument it receives. Arguments within functions are treated in the same manner as arguments given to the script.
Script Comments
Using comments in a script will make the script more understandable. Comments are like inline documentation for the script. Anyone reading the script can easily understand each step if it has comments included. Comments will be ignored when the script executes. A single line can be commented very easily by inserting a hash tag - # - in the line. Anything after the hash tag until the end of the line is a comment. It is also possible to have comments across multiple lines.
Using an earlier script example, we can show single line comments:
#!/bin/bash
# Set the Dir variable to some directory
Dir=~/testdir
# Test to see if the mkdir command completes successfully
# IF it succeeds - then does the cd command complete successfully
if [ "mkdir $Dir" ] && [ "cd $Dir" ]
then
echo "$Dir was not created." # We print a failure message
fi
To create a multi-line comment the syntax is a bit odd, but it works just as well:
#!/bin/bash
# Set the Dir variable to some directory
Dir=~/testdir
<<MYCOMMENT-1
Test to see if the mkdir command completes successfully
IF it succeeds - then does the cd command complete successfully
MYCOMMENT-1
if [ "mkdir $Dir" ] && [ "cd $Dir" ]
then
echo "$Dir was not created." # We print a failure message
fi
The multi-line comment MUST begin with <<, followed by some sequence of characters.There is NOTHING special about the MYCOMMENT-1 - you can use any sequence of characters as a delimiter, it just has to be the same at the beginning and the end.
Adapted from:
"3.3. Quoting characters" by Multiple Contributors, The Linux Documentation Project is licensed under CC BY-SA 3.0
"10.2. Array variables" by Multiple Contributors, The Linux Documentation Project is licensed under CC BY-SA 3.0