Linux Shell Scripting – How to Read a File and Redirect to a Variable

linuxscriptingshell

I have a file with a word written on it. I want my script to put that word in a variable.

How can I do that?

Best Answer

in several of a million ways...

simplest is probably

my_var=$(cat my_file)

If you use bash and you want to get spiffy you can use bash4's mapfile, which puts an entire file into an array variable, one line per cell

mapfile my_var < my_file
Related Question