How can I create a file using the cat
command with a here-document 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 "}")
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