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?
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
The simplest way is probably:
var=$(< file)
which doesn't create a new process.
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.
I think the easiest way is something like
$ myvar=`cat file`
I think it will strip newlines, but here it is anyway:
variable=$(cat filename)