Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-18-2005, 06:39 AM   #1
Registered: Feb 2004
Location: Linuxland
Distribution: Ubuntu 5.10, KUbuntu 5.10, Mandrake 10.1 - Ubuntu 5.04 (Hoary)
Posts: 346

Rep: Reputation: 30
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?
Old 12-18-2005, 06:40 AM   #2
Senior Member
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,544
Blog Entries: 4

Rep: Reputation: 57
kernel=`cat /home/usertest/file1`
Backticks are your answer
Old 12-18-2005, 08:36 AM   #3
Registered: Aug 2003
Posts: 150

Rep: Reputation: 15
Yup could also shorten this to
kernel=`uname -r`
echo "Your kernel is" $kernel
Old 12-19-2005, 05:20 AM   #4
Registered: Dec 2004
Location: Québec
Distribution: Gentoo, Kubuntu Karmic
Posts: 48

Rep: Reputation: 15
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
Old 12-19-2005, 01:41 PM   #5
Registered: Feb 2004
Location: Linuxland
Distribution: Ubuntu 5.10, KUbuntu 5.10, Mandrake 10.1 - Ubuntu 5.04 (Hoary)
Posts: 346

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 05:48 PM
get file contents with newlines into bash variable otoomet Linux - Software 2 01-06-2005 01:23 PM
Bash - Using variable from a .conf file steevy18 Programming 1 10-28-2003 11:58 AM
how to add environment variable to .bashrc file hasan Linux - General 7 10-03-2003 04:43 PM
how to add environment variable to .bashrc file? hasan Red Hat 1 10-03-2003 01:37 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:28 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration