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

LinuxSeeker 12-18-2005 07: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 07:40 AM


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

zeos 12-18-2005 09:36 AM

Yup could also shorten this to

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

denver1980 12-19-2005 06: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 02: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 10:38 PM.