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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.