LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Add file content to a variable (bash)? (http://www.linuxquestions.org/questions/programming-9/add-file-content-to-a-variable-bash-394128/)

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:

Code:

#!/bin/bash
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

Code:

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

zeos 12-18-2005 08:36 AM

Yup ...you could also shorten this to
Code:

#!/bin/bash
kernel=`uname -r`
echo "Your kernel is" $kernel


denver1980 12-19-2005 05:20 AM

Want to do a one-liner ?
Code:

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

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

# 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 09:09 PM.