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?
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 echo X > f; mapfile -t V < f; echo V is $V; export V; env | grep V=. Note how V is missing from env despite being defined before exporting.The simplest way is probably:
var=$(< file) which doesn't create a new process.
(<” instead of “( <”).var="`cat /path/to/file`" This is the simple way. Be careful with newlines in the file.
var="`head -1 /path/to/file`" This will only get the first line and will never include a newline.
$x="`cat log`" results in =2: command not foundI think the easiest way is something like
$ myvar=`cat file` I think it will strip newlines, but here it is anyway:
variable=$(cat filename) echo $variable vs. echo "$variable"