Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-21-2012, 05:36 AM
|
#1
|
LQ Newbie
Registered: Nov 2011
Posts: 18
Rep:
|
Assign Command value to a variable
Dear All
i have a query that , i want to assign a value as date to a variable like that
var1=$(date)
but not working
need help.
thanks in advance for help
|
|
|
05-21-2012, 05:41 AM
|
#2
|
Member
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719
Rep:
|
Wat exactly is the issue ?
Code:
[joe@Tux-Box ~]$ var=$(date)
[joe@Tux-Box ~]$ echo $var
Mon May 21 05:40:52 EDT 2012
This approach works.
|
|
|
05-21-2012, 06:07 AM
|
#3
|
LQ Newbie
Registered: Nov 2011
Posts: 18
Original Poster
Rep:
|
i want to make an auto generate file like that
21-05-12.txt
22-05-12.txt
but it does not like as i want.
Thanks for your response
|
|
|
05-21-2012, 06:13 AM
|
#4
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Does the date command work at the command prompt?
EDIT:
If you put type date in the script on the line before var1=$(date), what is the output?
Last edited by catkin; 05-21-2012 at 06:15 AM.
|
|
|
05-21-2012, 06:31 AM
|
#5
|
LQ Newbie
Registered: Nov 2011
Posts: 18
Original Poster
Rep:
|
type date
var1=$(date)
output is:
date is hashed (/bin/date)
Mon May 21 16:28:32 BDT 2012
|
|
|
05-21-2012, 06:40 AM
|
#6
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by rabir
type date
var1=$(date)
output is:
date is hashed (/bin/date)
Mon May 21 16:28:32 BDT 2012
|
The "date is hashed (/bin/date)" looks good.
Is the "Mon May 21 16:28:32 BDT 2012" output from the script or from running the date command at the command prompt?
|
|
|
05-21-2012, 06:41 AM
|
#7
|
LQ Newbie
Registered: Nov 2011
Posts: 18
Original Poster
Rep:
|
when write type date
then output is :
date is hashed (/bin/date)
Last edited by rabir; 05-21-2012 at 06:47 AM.
|
|
|
05-21-2012, 06:52 AM
|
#8
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by rabir
type date
var1=$(date)
output is:
date is hashed (/bin/date)
Mon May 21 16:28:32 BDT 2012
|
Is the "Mon May 21 16:28:32 BDT 2012" output from the script or from running the date command at the command prompt?
|
|
|
05-21-2012, 07:18 AM
|
#9
|
Member
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267
Rep:
|
If you want to use it as a file name, it is better not to have blanks in it. date has many different formats here's just one:
Code:
]$ var1=$(date +%F); echo $var1
2012-05-21
]$
Last edited by david1941; 05-21-2012 at 07:19 AM.
|
|
|
05-21-2012, 07:24 AM
|
#10
|
LQ Addict
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464
Rep:
|
So, is the question about assigning a command value to a variable, or about date's formats? rabir, please clarify. If the latter, see the man page for info on how to format the output from date as you want.
|
|
|
05-21-2012, 10:01 AM
|
#11
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
Quote:
Originally Posted by rabir
i want to make an auto generate file like that
21-05-12.txt
22-05-12.txt
but it does not like as i want.
Thanks for your response
|
e.g.:
Code:
touch `date +%Y-%j.txt`
man date for more info.
|
|
|
All times are GMT -5. The time now is 06:49 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|