Find the answer to your Linux question:
Results 1 to 4 of 4
In a script using bash, I would like to assign the contents of a file to a variable. How can I do this in a simple manner?...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2004
    Location
    New Mexico
    Posts
    2

    Assigning file contents to variable


    In a script using bash, I would like to assign the contents of a file to a variable. How can I do this in a simple manner?

  2. #2
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    For example by:
    Code:
    varname=`cat file.txt`
    note, there are no spaces around assignment and I'm using backstics (below tilde char) to execute cat file.txt and return its output.
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  3. #3
    Just Joined!
    Join Date
    Aug 2004
    Location
    New Mexico
    Posts
    2
    Thanks, kyku. I had seen this solution before but just assumed that the backsticks were single quotes italicized.

  4. #4
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    As an end matter, there's a clearer solution: $(progname arg1 arg2 ... argn)
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •