Need to assign the contents of a text file to a variable in a bash script

Bash

Bash Problem Overview


I am very new to making bash scripts, but my goal here is to take a .txt file I have and assign the string of words in the txt file to a variable. I have tried this (no clue if I am on the right track or not).

#!/bin/bash
FILE="answer.txt"
file1="cat answer.txt"
print $file1

When I run this, I get

Warning: unknown mime-type for "cat" -- using "application/octet-stream"
Error: no such file "cat"
Error: no "print" mailcap rules found for type "text/plain"

What can I do to make this work?

Edit** When I change it to:

#!/bin/bash
    FILE="answer.txt"
    file1=$(cat answer.txt)
    print $file1

I get this instead:

Warning: unknown mime-type for "This" -- using "application/octet-stream"
Warning: unknown mime-type for "text" -- using "application/octet-stream"
Warning: unknown mime-type for "string" -- using "application/octet-stream"
Warning: unknown mime-type for "should" -- using "application/octet-stream"
Warning: unknown mime-type for "be" -- using "application/octet-stream"
Warning: unknown mime-type for "a" -- using "application/octet-stream"
Warning: unknown mime-type for "varible." -- using "application/octet-stream"
Error: no such file "This"
Error: no such file "text"
Error: no such file "string"
Error: no such file "should"
Error: no such file "be"
Error: no such file "a"
Error: no such file "varible."

When I enter cat answer.txt it prints out this text string should be a varible like it should but, I still can't get the bash to do that with the varible.

Bash Solutions


Solution 1 - Bash

You need the backticks to capture output from a command (and you probably want echo instead of print):

file1=`cat answer.txt`
echo $file1

Solution 2 - Bash

In bash, $ (< answer.txt) is equivalent to $ (cat answer.txt), but built in and thus faster and safer. See the bash manual.

I suspect you're running this print:

NAME  
    run-mailcap, see, edit, compose, print − execute programs via entries in the mailcap file

Solution 3 - Bash

The $() construction returns the stdout from a command.

file_contents=$(cat answer.txt)

Solution 4 - Bash

Use command substitution with caution because it removes trailing new lines that makes the variable and the input file not identical, for example:

$ printf 'a\n\n' > input.txt
$ md5 < input.txt
94364860a0452ac23f3dac45f0091d81
$ x=$(cat input.txt)
$ printf %s "$x" | md5
0cc175b9c0f1b6a831c399e269772661
$ printf %s "$x" | od -ta
0000000    a                                                            
0000001

To make the file and the variable identical, add an extra byte and then remove it later:

$ x=$(cat input.txt && echo .)
$ x=${x%.}
$ printf %s "$x" | md5
94364860a0452ac23f3dac45f0091d81
$ printf %s "$x" | od -ta
0000000    a  nl  nl                                                    
0000003

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBluGeniView Question on Stackoverflow
Solution 1 - BashJeffrey TheobaldView Answer on Stackoverflow
Solution 2 - Bashglenn jackmanView Answer on Stackoverflow
Solution 3 - BashharpoView Answer on Stackoverflow
Solution 4 - BashWeihang JianView Answer on Stackoverflow