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 SolutionsShell – How to Read a File into a Variable In cross-platform, lowest-common-denominator sh you use: #!/bin/sh value=`cat config.txt` echo "$value" In bash or zsh, to read a whole file into a variable without invoking cat: #!/bin/bash value=$(<config.txt) echo "$value" Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of Cat. Note that it is not necessary to quote the command substitution to preserve newlines. See: Bash Hacker's Wiki - Command substitution - Specialities. Related Question
Best Answer
in several of a million ways...
simplest is probably
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