-   Programming (
-   -   Add file content to a variable (bash)? (

LinuxSeeker 12-18-2005 06:39 AM

Add file content to a variable (bash)?
I am trying to create a simple shell script that will print the following message on the screen: "Your kernel is: (kernel version)".

My script is:


yks="Your kernel is: "
kernel=cat /home/usertest/file1
#this is the text file in which I have saved my kernel version
echo $yks $kernel

But it seems that I cannot assign the cat command to a variable. Any suggestions?

cs-cam 12-18-2005 06:40 AM


kernel=`cat /home/usertest/file1`
Backticks are your answer :)

zeos 12-18-2005 08:36 AM

Yup could also shorten this to

kernel=`uname -r`
echo "Your kernel is" $kernel

denver1980 12-19-2005 05:20 AM

Want to do a one-liner ?

echo "Your kernel is `uname -r`"
Want to execute it anytime-anywhere ?
Add an alias in you ~/.profile

alias Ver="echo \"Your kernel is `uname -r`\"
Invoke it by typing :

# Ver

LinuxSeeker 12-19-2005 01:41 PM

Thank you all. What I mainly wanted was to know how to use 'cat' in a variable but your answers were even better :)

All times are GMT -5. The time now is 06:27 PM.