[PLUG] pulling data from a file to a variable

Paul Heinlein heinlein at madboa.com
Wed Jun 16 05:55:03 UTC 2004


On Tue, 15 Jun 2004, Richard Kurth wrote:

> How can I get the data that is in a file into a variable at the 
> Command line. The file only has one line it it and I what to read 
> the file and add that one line to a variable. I know cat file_name 
> will show the data to the screen so I tried VAR1=[cat file_name] but 
> that does not work.

See the 'Command Substitution' section of the bash(1) man page, but in 
short...

   # the old-fashioned way
   VAR1=`cat file_name`

   # the posix way
   VAR1=$(cat file_name)

   # faster version of above
   VAR1=$(< file_name)

   # in case your file has multiple lines
   VAR1=$(head -n 1 file_name)

-- Paul Heinlein <heinlein at madboa.com>




More information about the PLUG mailing list