read a word from a file in linux shell script

hi,
I have a file named env
It contains only one word : staging/prod/...
I just want to read that word inside the file..
whats the best and easiest way to do it ?

Thanks
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

simon3270Commented:
With the full path to the file:

    fred=$(cat /path/to/env)

or if you know it is in the current directory:

    fred=$(cat env)

An alternative, available in bash but maybe not elsewhere, is:

    fred=$(<env)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rohit BajajAuthor Commented:
thanks.
just one more thing
if there are two or say may be 3 or more
and i want to store each of them in different variables and use them later..
i am asking for 3 or more just for knowledge sake.
but my current case will probably require 2

first one will be stored in a variable flock-apps-xonfig
and second in port
just want to know also if there are multiple such lines then how to do it using array
simon3270Commented:
It would be better if they were in the form you first used (e.g. PORT=8080) so you could use the awk statement, so the order of lines in the file would not matter.

If they are just the first, second and third lines, one way would be:
    flock_apps_xconfig=$(sed -n 1p env)
    port=$(sed -n 2p env)
    nextvar=$(sed -n 3p env)
(the "-n" stops sed printing out each line, and the "1p" means print out line 1).  You can do the same think with awk, as:
    port=$(awk 'NR == 2{print}')
but sed is a more lightweight program (I hate wasting CPU cycles!).

Another option, if you are sure that each line has just one word on it (no spaces) you can do
    set -- $(cat env)
That sets $1 to the value of the first line, $2 to the second and so on.

By the way, flock-apps-xonfig is not a valid variable name - hyphens now not allowed (they were in some early shells).

Edit: I was thinking of dots here - I don't think hyphens have ever been allowed
simon3270Commented:
If you want to use an array, you could do something like:
   rsparr=($(cat env))
then use ${rsparr[0]} for the first line, ${rsparr[1]} for the second, ${rsparr[2]} for the third and so on.  This also asumes that each line has just one word on it.  The "sed" and "awk" solutions above work with lines, however many words there are on the line.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.