LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 12-18-2005, 06:39 AM   #1
LinuxSeeker
Member
 
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:

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?
 
Old 12-18-2005, 06:40 AM   #2
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,545

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

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

Rep: Reputation: 15
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
 
Old 12-19-2005, 01:41 PM   #5
LinuxSeeker
Member
 
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
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:50 AM.

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