Brash Strings

In brash, everything of interest is one of the following: A statement is the mechanism by which files, scripts, and programs are accessed. A statement is a collection of strings. Files usually are thought of as having strings in them. So, string is the be-all and end-all of the brash world.

Types of Strings

Here are the types of strings that can be defined in a brash script:
unprotectedfred
double quote protected"fred"
single quote protected'fred'
subshell quoted`echo fred`
recursive-subshell quoted$(echo $(echo $fred))
Note the difference in the quoting of the single quote protected and the unprotected subshell quotes.

Protected verses Unprotected strings

Strings can either be protected or unprotected. The word "protected" in this case means:
Protected against splitting.
There is a variable, IFS, that defines a string which contains space, tab, and newline. The characters in this set define "substring separators". On command lines, whenever a string is evaluated, it will be automatically split into multiple pieces using the set of characgters defined by IFS. Here is an example script that demonstrates this: fred="a b c d" for f in $fred do echo $f done In this example, four lines of output will be produced: a b c d These 4 lines occur because of the automatic splitting of the expression $fred.

Note that if the expression $fred had been protected, different results would have occurred. Here are some examples:

Subshell Quoting Schemes

Brash, bash, and the bourne shell all allow you to invoke commands and have either stdout contents be treated as a single string. There two ways of doing this: The reverse-apostrophe method works like this: the command inside the quotes must not contain the reverse-apostorphe character. Whatever is inside is treated as a command and the result returned as a single string to brash. Those results will be further split using the IFS variable's contents.

The recursive subshell quotes, $(...), work the same -- except that it is possible to have $(...) within an outer $(...). Basically, this is the mechanism for getting recursion to work with subshells.

The commands that are invoked by subshells can be any brash statement -- which means they can be if-statements, function invocations, eternal command invocations, or whatever is needed.

The standard error file output will not be included in the returned string unless the command is written in such a way as to merge stdout and standard error. For example:

x=$(program 2>&1) In this case, both the program's normal outputs and its error messages will go into variable x.

String Functions

Much of the power of brash, bash, and the bourne shell as compared to the windows command processor is its ability to perform complex string manipulations and checks.

The windows command processor does some string manipulation capbility. For example:


     echo %varnameoperation%

Where the operations are fairly helpful -- but the availability of the operations, in various syntaxes of the language are highly inconsistent. This makes them painful to use. In brash, bash, and the bourne shell, string evaluation is the same all the time, every where -- even in inline file data

Like the windows command processor, the basic idea is to populate an environment variable some string, then use a string expression that performs some sort of operation on it. String expressions begin with a dollar sign and involve variable names and other characters.

The string expressions in brash, bash, and bourne are fairly cryptic. Practice is required -- as well as having google handy to figure out which function you need.

The following table defines the most common operations:
nameExpressionExplanation
command line argument$4Get command line argument 4
command line argument${4}Get command line argument 4
count of arguments${#*}Get the count of command line arguments.
count of arguments${#@}Get the count of command line arguments.
count of arguments$#*Get the count of command line arguments.
count of arguments$#@Get the count of command line arguments.
constant stringsfred bill sue3 unprotected string constants
protected constant strings"fred" ' bill '2 protected string constants
a protected constant string'$fred "$fred" $(echo $fred)'1 string constant
simple expressions$fred "$fred" $(echo $fred)3 string expressions
simple variable expansion$fredA simple variable expansion
simple variable expansion${fred}Another simple variable expansion
remove shortest trailing pattern${fred%/*}Evalates to the contents of variable fred, but first removes everything after the final / in fred's contents. For example: if fred contains /home/fred/bill, then expression would evaluate to /home/fred.
remove longest trailing pattern${fred%%/*}Evalates to the contents of variable fred, but first removes everything after the first / in fred's contents. For example: if fred contains /home/fred/bill, then expression would evaluate to empty string.
remove shortest leading pattern${fred#/*}Evalates to the contents of variable fred, but first removes everything after the final / in fred's contents. For example: if fred contains /home/fred/bill, then expression would evaluate to home/fred/bill.
remove longest leading pattern${fred##/*}Evalates to the contents of variable fred, but first removes everything after the first / in fred's contents. For example: if fred contains /home/fred/bill, then expression would evaluate to empty string.
replace first pattern match${fred\//-}Evalates to the contents of variable fred, but first removes everything after the first / in fred's contents. For example: if fred contains /home/fred/bill, then expression would evaluate to -home/fred/bill.
replace all pattern matches${fred\//-}Evalates to the contents of variable fred, but first removes everything after the first / in fred's contents. For example: if fred contains /home/fred/bill, then expression would evaluate to -home-fred-bill.
uppercase the first letter${fred^}Upper case the first letter of the contents of fred. For example, if fred contains "lower", then expression evaluates to Lower.
uppercase all${fred^^}Upper case all letters in the contents of fred. For example, if fred contains "lower", then expression evaluates to LOWER.
lowercase the first letter${fred,}Lower case the first letter of the contents of fred. For example, if fred contains "UPPER", then expression evaluates to uPPER.
lowercase all${fred,,}Lower case all letters in the contents of fred. For example, if fred contains "UPPER", then expression evaluates to upper.
string length${#fred}Get the length of a variable's conents. For example if fred containts SOS, then expression evaluates to 3