Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

create a file using cat command inside bash script

How to create a file using cat command inside shell script

#!/bin/bash

create_file () {
    cat > a.txt << EOF
    0 abc def 
    ghi jkl mno
    pqrs tuv wxyz
    EOF
}

create_file

Error

Syntax error: end of file unexpected (expecting "}")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

remove indentation after opening EOF:

#!/bin/bash

create_file () {
    cat > a.txt <<EOF
0 abc def 
ghi jkl mno
pqrs tuv wxyz
EOF
}

create_file

Bash looks for the same "delimiter" that you started with. You started with "EOF", but was ending with "____EOF" when using indentation, so it couldn’t match closing delimiter

if you want add spacing for the resulting text, add indentation to the text, but keep EOF at the start of the line:

#!/bin/bash

create_file () {
    cat > a.txt <<EOF
  0 abc def 
  ghi jkl mno
  pqrs tuv wxyz
EOF
}

create_file

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading