unprotected | fred |
---|---|
double quote protected | "fred" |
single quote protected | 'fred' |
subshell quoted | `echo fred` |
recursive-subshell quoted | $(echo $(echo $fred)) |
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:
Note that if the expression $fred had been protected, different results would have occurred. Here are some examples:
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:
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:
name | Expression | Explanation |
---|---|---|
command line argument | $4 | Get 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 strings | fred bill sue | 3 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 | $fred | A 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 |