The here operator with the sort command

Let's write a script for using the sort command along with the here document:

  1. Write the script here_02.sh as follows:
#!/bin/bash 
sort << EOF 
cherry 
mango 
apple 
banana 
EOF 
  1. Save the file, give the permission to execute, and run the script as follows:
    $ chmod u+x here_02.sh
    $ ./here_02.sh
  
  1. The output is here:
    apple
    banana
    cherry
    mango
  

In this script, the here document is enclosed between the EOF pattern. We have used the here document to supply text to the sort command.

Get Learning Linux Shell Scripting - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.