Storing the contents of a file in a variable

There is a file named file.txt whose contents are:

+-----------------------------------+-----------+
| Variable_name                     | Value     |
+-----------------------------------+-----------+
| Aborted_clients                   | 0         |
| Aborted_connects                | 25683     |
| Binlog_cache_disk_use          | 0         |
| Binlog_cache_use                 | 0         |
| Bytes_received                    | 30308258  |
| Bytes_sent                         | 519327296 |
| Com_admin_commands          | 0         |
| Com_alter_db                      | 0         |
| Com_alter_table                   | 228       |
| Com_analyze                       | 0         |
| Com_backup_table               | 0         |
| Com_begin                          | 19779     |
| Com_call_procedure              | 0         |
| Com_change_db                   | 10380     |
| Com_change_master             | 0         |
| Com_check                          | 0         |
| Com_checksum                     | 0         |
| Com_commit                        | 15414     |
| Com_create_db                    | 3         |
| Com_create_function             | 0         |
| Com_create_index                 | 0         |
| Com_create_table                 | 114       |
| Com_create_user                  | 0         |
| Com_dealloc_sql                   | 0         |
| Com_delete                         | 9470      |
| Com_delete_multi                  | 0         |
| Com_do                               | 0         |
| Com_drop_db                       | 0         |
| Com_drop_function                | 0         |
| Com_drop_index                    | 0         |
| Com_drop_table                    | 114       |
| Com_drop_user                     | 0         |
| Com_execute_sql                   | 0         |
| Com_flush                            | 4         |
| Com_grant                           | 3         |
| Com_ha_close                       | 0         |
| Com_ha_open                       | 0         |

I want to store the contents of this file to a variable named varname.
I am using the command:

varname=`cat file.txt`

But when i am running:

echo $varname

The output is:

+-----------------------------------+-----------+ | Variable_name | Value | +-----------------------------------+-----------+
| Aborted_clients | 0 | | Aborted_connects | 25682 | | Binlog_cache_disk_use | 0 | | Binlog_cache_use | 0 | | Bytes_received |
 30308028 | | Bytes_sent | 519314110 | | Com_admin_commands | 0 | | Com_alter_db | 0 | | Com_alter_table | 228 | | Com_analyze
 | 0 | | Com_backup_table | 0 | | Com_begin | 19779 | | Com_call_procedure | 0 | | Com_change_db | 10380 | | Com_change_master
 | 0 | | Com_check | 0 | | Com_checksum | 0 | | Com_commit | 15414 | | Com_create_db | 3 | | Com_create_function | 0 | | Com_c
reate_index | 0 | | Com_create_table | 114 | | Com_create_user | 0 | | Com_dealloc_sql | 0 | | Com_delete | 9470 | | Com_delet
e_multi | 0 | | Com_do | 0 | | Com_drop_db | 0 | | Com_drop_function | 0 | | Com_drop_index | 0 | | Com_drop_table | 114 | | C
om_drop_user | 0 | | Com_execute_sql | 0 | | Com_flush | 4 | | Com_grant | 3 | | Com_ha_close | 0 | | Com_ha_open | 0 | | Com_
ha_read | 0 | | Com_help | 0 | | Com_insert | 5446 | | Com_insert_select | 10 | | Com_kill | 0 | | Com_load | 0 | | Com_load_m
aster_data | 0 | | Com_load_master_table | 0 | | Com_lock_tables | 271 | | Com_optimize | 0 | | Com_preload_keys | 0 | | Com_p
repare_sql | 0 | | Com_purge | 0 | | Com_purge_before_date | 0 | | Com_rename_table | 0 | | Com_repair | 0 | | Com_replace | 1
60 | | Com_replace_select | 0 | | Com_reset | 0 | | Com_restore_table | 0 | | Com_revoke | 0 | | Com_revoke_all | 0 | | Com_ro
llback | 7 | | Com_savepoint | 0 | | Com_select | 75393 | | Com_set_option | 25888 | | Com_show_binlog_events | 0 | | Com_show
_binlogs | 38 | | Com_show_charsets | 188 | | Com_show_collations | 188 | | Com_show_column_types | 0 | | Com_show_create_db |
 196 | | Com_show_create_table | 5133 | | Com_show_databases | 237 | | Com_show_errors | 0 | | Com_show_fields | 5244 | | Com_
show_grants | 85 | | Com_show_innodb_status | 0 | | Com_show_keys | 45 | | Com_show_logs | 0 | | Com_show_master_status | 0 |
| Com_show_ndb_status | 0 | | Com_show_new_master | 0 | | Com_show_open_tables | 0 | | Com_show_privileges | 0 | | Com_show_pr
ocesslist | 3 | | Com_show_slave_hosts | 0 | | Com_show_slave_status | 0 | | Com_show_status | 24421 | | Com_show_storage_engi
nes | 0 | | Com_show_tables | 547 | | Com_show_triggers | 5109 | | Com_show_variables | 12637 | | Com_show_warnings | 0 | | Co
m_slave_start | 0 | | Com_slave_stop | 0 | | Com_stmt_close | 0 | | Com_stmt_execute | 0 | | Com_stmt_fetch | 0 | | Com_stmt_p
repare | 0 | | Com_stmt_reset | 0 | | Com_stmt_send_long_data | 0 | | Com_truncate | 0 | | Com_unlock_tables | 309 | | Com_upd
ate | 2901 | | Com_update_multi | 0 | | Com_xa_commit | 0 | | Com_xa_end | 0 | | Com_xa_prepare | 0 | | Com_xa_recover | 0 | |

How can i store the contents of the file in the same format as it it there in the file?

You sould quote the variable:

printf '%s\n' "$varname"

You could also avoid using cat with most shells:

varname="$(<filename)"
You could also avoid using cat with most shells

What is the reason behind that?

You may want to enclose the variable in double quotes ...

[house@leonov] data=$( cat in.file ); echo "$data"
+-----------------------------------+-----------+
| Variable_name                     | Value     |
+-----------------------------------+-----------+

Here's the reason.

Avoid using an external command because:

  • the builtin sould be faster
  • you avoid unnecessary forks

Thanks, worked like a charm. Can you explain a bit

---------- Post updated at 11:24 AM ---------- Previous update was at 11:21 AM ----------

OK got it. Thanks everybody for the help.