FreeKB - Bash (Scripting) Create Excel or CSV file
Bash (Scripting) - Create Excel or CSV file

Redirection is all that is needed to create a CSV file in a bash shell script. In this example, a CSV file named example.csv is created, and the text Hello World will be in cell A1 of the CSV file.

~]# echo "Hello World" > example.csv


Comma separate

The data in a CSV file is often separated by a comma. Simply placing a comma between Hello and World will separated the data with a comma so that the data is in unique cells.

~]# echo "Hello,World" > example.csv


Or, to do the same in a bash script.



echo $foo,$bar > example.csv


Let's say you want data in the cells to be separated by a new line.


This trick is simply to place double quotes around the data being passed to the CSV file.


foo="Line 1
Line 2
Line 3"

echo "$foo" > example.csv


Tab separate

The column command can be used to create a tab separated CSV file. In this example, the data in the example.csv file is comma separated, and then a new CSV file named tab.csv is created.

~]# column -t example.csv > tab.csv

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 04e1b in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |