Skip to content
Fix Code Error

How does “cat

March 13, 2021 by Code Error
Posted By: Anonymous

I needed to write a script to enter multi-line input to a program (psql).

After a bit of googling, I found the following syntax works:

cat << EOF | psql ---params
BEGIN;

`pg_dump ----something`

update table .... statement ...;

END;
EOF

This correctly constructs the multi-line string (from BEGIN; to END;, inclusive) and pipes it as an input to psql.

But I have no idea how/why it works, can some one please explain?

I’m referring mainly to cat << EOF, I know > outputs to a file, >> appends to a file, < reads input from file.

What does << exactly do?

And is there a man page for it?

Solution

This is called heredoc format to provide a string into stdin. See https://en.wikipedia.org/wiki/Here_document#Unix_shells for more details.


From man bash:

Here Documents

This type of redirection instructs the shell to read input from
the current source until a line
containing only word (with no trailing
blanks) is seen.

All of the lines read up to that point are then used as the
standard input for a command.

The format of here-documents is:

          <<[-]word
                  here-document
          delimiter

No parameter expansion, command substitution, arithmetic expansion, or
pathname expansion is performed on
word. If any characters in word are
quoted, the
delimiter is the result of quote removal on word, and the lines
in the here-document are not expanded.
If word is unquoted, all lines of the
here-document are subjected to parameter expansion, command
substitution, and arithmetic
expansion. In the latter case, the
character sequence <newline> is
ignored, and must be used to quote the characters , $, and `.

If the redirection operator is <<-, then all leading tab characters
are stripped from input lines and the
line containing delimiter. This
allows here-documents within shell scripts to be indented in a natural fashion.

Answered By: Anonymous

Related Articles

  • Multiple separate IF conditions in SQL Server
  • data.table vs dplyr: can one do something well the…
  • How to properly do JSON API GET requests and assign…
  • Maximum XOR With an Element From Array | Leetcode
  • PHP parse/syntax errors; and how to solve them
  • Can I access results of "setup_data" from…
  • Is CSS Turing complete?
  • Copying PostgreSQL database to another server
  • Postgresql 9.2 pg_dump version mismatch
  • What's the difference between eval, exec, and compile?
  • Why I can't read this file in Common Lisp?
  • Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?
  • What are the undocumented features and limitations…
  • Why does C++ code for testing the Collatz conjecture…
  • Simplest way to create Unix-like continuous pipeline…
  • Sort table rows In Bootstrap
  • Ubuntu apt-get unable to fetch packages
  • How does ifstream's eof() work?
  • RegEx match open tags except XHTML self-contained tags
  • SQL query return data from multiple tables
  • Java Process with Input/Output Stream
  • C++ Need help sorting a 2D string array
  • PostgreSQL psql terminal command
  • postgresql port confusion 5433 or 5432?
  • Angular 4 HttpClient Query Parameters
  • How to access params in the afterModel hook
  • C++ cast to derived class
  • How to use html template with vue.js
  • how to make Select function used in model…
  • I want to solve the javascript OOP problems, but my…
  • AppCompat v7 r21 returning error in values.xml?
  • End of File (EOF) in C
  • How do I register the on-tap in a paper-icon-button…
  • Why is “while ( !feof (file) )” always wrong?
  • Smart way to truncate long strings
  • How can I write a heredoc to a file in Bash script?
  • psql: FATAL: database "" does not exist
  • How to combine the data from two different…
  • How to avoid exhaustive matching and directly return…
  • How to end C++ code
  • Using Auto Layout in UITableView for dynamic cell…
  • Ukkonen's suffix tree algorithm in plain English
  • When should I use a table variable vs temporary…
  • What does if __name__ == "__main__": do?
  • Set the absolute position of a view
  • Oracle: If Table Exists
  • Using pg_dump to only get insert statements from one…
  • How do I use shell variables in an awk script?
  • Programmatically generate url from path and params
  • Postgres could not connect to server
  • How can I fix MySQL error #1064?
  • psql: command not found Mac
  • Postgres password authentication fails
  • VueJS trigger Checkbox by clicking table row
  • Bootstrap-Table: Using Ajax call and add link to…
  • Aurelia - If params in Promise.all
  • psql: FATAL: role "postgres" does not exist
  • Cleanest way of checking if any of 5 variables exist…
  • I can't get data of my table with bootstrap…
  • Grouping by similar lists in a column within a dataframe
  • What are named pipes?
  • What does this symbol mean in JavaScript?
  • What is Common Gateway Interface (CGI)?
  • Getting infinite loop after entering 2 objects to…
  • Why do we need virtual functions in C++?
  • Why does getline reads only the first stroke?
  • What is and how to fix…
  • Logging best practices
  • Typescript/javascript method being invoked before…
  • C++ template,typename and operator
  • Improve INSERT-per-second performance of SQLite
  • Multi-statement Table Valued Function vs Inline…
  • Android Linear Layout Parameters
  • Detecting EOF without 0-byte read in Rust
  • How do I login and authenticate to Postgresql after…
  • How to install Intellij IDEA on Ubuntu?
  • Why Polmyer iron-ajax will not auto request when…
  • Alter table add multiple columns ms sql
  • How to apply filters to *ngFor?
  • Is there a way to treat React components as strings?
  • Where and why do I have to put the "template" and…
  • Access Crypto.Com API with R (translate from Python)
  • How to cat a file containing code?
  • Generating random whole numbers in JavaScript in a…
  • What is your most productive shortcut with Vim?
  • How to define relations between models with a table…
  • python wikipedia package changing input
  • Iterator invalidation rules
  • Postman gives 401 Unauthorized - Spring Boot & MYSQL
  • T-SQL How to create tables dynamically in stored procedures?
  • Fastest way to iterate over all the chars in a String
  • Vue JS - How do I get WebGL to render via Vue…
  • Vuex update array as value of object
  • Typeerror: n is undefined in underscore.min.js
  • Log record changes in SQL server in an audit table
  • Start redis-server with config file
  • Confused about the Visitor Design Pattern
  • Can't start Eclipse - Java was started but returned…
  • Bash: Echoing a echo command with a variable in bash
  • Reading from text file until EOF repeats last line

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I create an empty array/matrix in NumPy?

Next Post:

Why am I seeing “TypeError: string indices must be integers”?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error