How can the white space character be escaped in printf for the purpose of this script that currently runs in a Cygwin client? The line containing the whitespace (which by itself works from the command line as echo $SPC) is
SPC=$(printf \\$(printf '%03o' 32))
In case you might wonder why, in this code I am trying to simulate an associative array such that it is not dependent on Bash 4+...would like this to be a generic example that theoretically can be run in virtually any bash environment.
Here is the entire script:
#!/bin/bash
DEFAULT_INDEX=""
LAST_INDEX=0
SPC=$(printf \\$(printf '%03o' 32))
# MICROSOFT OUTLOOK VERSIONS.
OUTLOOKS=( "Cancel^Do$SPCnot$SPCtry$SPCto$SPCsend$SPCmessage$SPCvia$SPCOutlook"
"URL^Just$SPCthe$SPCmailto$SPCURL"
"Outlook$SPC2003^C:\Program$SPCFiles\Microsoft$SPCOffice\Office12"
"Outlook$SPC2007^C:\Program$SPCFiles\Microsoft$SPCOffice\Office12"
"Outlook$SPC2010^C:\Program$SPCFiles\Microsoft$SPCOffice\Office14" )
# ITERATES THROUGH ARRAY AND MAKES NUMBERED SELECTION LIST.
printOutlooksArraySelectionList() {
LIST_TITLE="MICROSOFT OUTLOOK SELECTION LIST"
echo
printf "\t\t\E[37;1;44m%-0s %s %s %s\033[0m" $LIST_TITLE
echo
echo
DEFAULT_VALUE="Outlook 2010"
index=0
for i in "${OUTLOOKS[@]}"; do
key=${i%%^*}
value=${i##*^}
if [ "$key" == "$DEFAULT_KEY" ]; then
DEFAULT_INDEX=$index
fi
printf "\t\E[37;44m%-3s %-13s\033[0m \E[1;34;40m%-41s\033[0m\n" \
$index $key $value
echo
((index++))
done
# SETS LAST_INDEX TO INDEX VALUE OF LAST ITEM IN ARRAY.
((LAST_INDEX = index - 1))
}
printOutlooksArraySelectionList
Would like the output to look like the following:
MICROSOFT OUTLOOK SELECTION LIST
0 Cancel Do not try to send message via Outlook
1 URL Just the mailto URL
2 Outlook 2003 C:\Program Files\Microsoft Office\Office12
3 Outlook 2007 C:\Program Files\Microsoft Office\Office12
4 Outlook 2010 C:\Program Files\Microsoft Office\Office14
To work-around, I ended up not using printf, but instead:
echo -e "\t\E[37;44m${index}\t${key}\033[0m\t\t\E[1;34;40m${value}\033[0m"
adequate for my purposes with this script.