Reputation: 1645
I'm writing a simple Bash script to spell check a single word via the command line using aspell.
The way to do this is:
echo "word" | aspell -a
Aspell then outputs something like:
@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.6.1)
*
or if the word is misspelled:
@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.6.1)
& wordd 29 0: word, wordy, worded, words, Ward, Wood, ward, woad, wood, wort, world, woody, wooed, weird, word's, Wed, wed, wot, we'd, who'd, odd, warded, wooded, worked, wormed, sword, wad, wordier, wold
My plan is to capture this output in a variable and output nothing if the word is correct or output the suggestions minus the Aspell version and spellcheck score if it is misspelled.
However, the contents of my current directory are strangely being added to the variable. Here's the script so far:
RESULTS=$(echo $1 | aspell -a)
echo $RESULTS
Giving the script an incorrectly-spelled word (i.e. setting $1 to a incorrectly-spelled word) gives me this:
@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.6.1) & shipp 41 0: ship, shoppe, shop, shipper, chippy, chip, ships, Sharp, chirp, shape, sharp, sheep, shopper, choppy, hippo, hippy, supp, chop, shipped, hip, sip, ship's, shops, chipper, chippie, Sharpe, Sherpa, chappy, chirpy, shpt, shim, shin, shit, shiv, whip, chap, chimp, chips, shier, shop's, chip's
(note it is all incorrectly on one line)
And giving a correctly-spelled word:
@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.6.1) Gemfile Gemfile.lock README.rdoc Rakefile app bin config config.ru db lib log public spell.sh test tmp vendor
strangely adds the contents of my current directory (Gemfile Gemfile.lock etc). Also note the newline is missing here too and the proper aspell output which contains an asterisk for correctly-spelled words is missing.
There's clearly something about Bash I'm not aware of. Anybody know what's going on?
Upvotes: 1
Views: 86
Reputation: 3125
You need to use double quotes in the echo
, in order to prevent the bash expansion of asterisk returned by the command aspell
:
echo "$RESULTS"
Try the following commands and watch the differences:
# echo "*"
# echo *
Read the section 3.5.8 Filename Expansion
in the bash manual pages
Upvotes: 3
Reputation: 185219
Try doing this :
RESULTS=$(echo "$1" | aspell -a | sed 1d)
[[ $RESULTS == \* ]] || echo "$RESULTS"
Upvotes: 0