Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

$(cat <

1. Assign multi-line string to a shell variable
$ sql=$(cat <<EOF
SELECT foo, bar FROM db
WHERE foo='baz'
EOF
)
The $sql variable now holds the new-line characters too. You can verify with echo -e "$sql".

2. Pass multi-line string to a file in Bash
$ cat <<EOF > print.sh
#!/bin/bash
echo $PWD
echo $PWD
EOF
The print.sh file now contains:

#!/bin/bash
echo $PWD
echo /home/user
3. Pass multi-line string to a pipe in Bash
$ cat <<EOF | grep 'b' | tee b.txt
foo
bar
baz
EOF
The b.txt file contains bar and baz lines. The same output is printed to stdout.

Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #heredoc #assign #command #variable
ADD COMMENT
Topic
Name
3+1 =