Here Strings

Advanced Bash-Scripting Guide: Prev Chapter 18. Here Documents Next A here string can be considered as a stripped-down form of a here document . It consists of nothing more than COMMAND <<<$WORD , where $WORD is expanded and fed to the stdin of COMMAND . As a simple example, consider this alternative to the echo-grep construction. # Instead of: if echo "$VAR" | grep -q txt # if [[ $VAR = *txt* ]] # etc. # Try: if grep -q "txt" <<< "$VAR" then echo "$VAR contains the substring sequence \"txt\"" fi # Thank you, Sebastian Kaminski, for the suggestion. Or, in combination with read : String="This is a string of words." read -r -a Words <<< "$String" # The -a option to "read" #+ assigns the resulting values to successive members of an array. echo "First word in String is: ${Words[0]}" # This echo "Second word in String is: ${Words[1]}" # is echo "Third word in...

Linked on 2014-05-29 23:03:13 | Similar Links