Pipe output of grep to a variable
You don't need to use grep at all:

[[ $(defaults read com.apple.finder AppleShowAllFiles) = 0 ]] &amp;&amp; bool=true || bool=false
defaults write com.apple.finder AppleShowAllFiles -bool $bool
osascript -e 'quit app "Finder"'


defaults read prints boolean values as 1 or 0. For example True or YES as a string is also interpreted as a boolean value, but -bool true specifies the value to be actually boolean.