LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 05-21-2012, 05:36 AM   #1
rabir
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Rep: Reputation: Disabled
Question 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
 
Old 05-21-2012, 05:41 AM   #2
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 106Reputation: 106
Question

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.
 
Old 05-21-2012, 06:07 AM   #3
rabir
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-21-2012, 06:13 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
 
Old 05-21-2012, 06:31 AM   #5
rabir
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
type date
var1=$(date)
output is:
date is hashed (/bin/date)
Mon May 21 16:28:32 BDT 2012
 
Old 05-21-2012, 06:40 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by rabir View Post
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?
 
Old 05-21-2012, 06:41 AM   #7
rabir
LQ Newbie
 
Registered: Nov 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
when write type date

then output is :
date is hashed (/bin/date)

Last edited by rabir; 05-21-2012 at 06:47 AM.
 
Old 05-21-2012, 06:52 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by rabir View Post
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?
 
Old 05-21-2012, 07:18 AM   #9
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
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.
 
Old 05-21-2012, 07:24 AM   #10
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
Old 05-21-2012, 10:01 AM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919
Quote:
Originally Posted by rabir View Post
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.
 
  


Reply


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 assign a string to a structure variable in C? daYz Programming 10 07-06-2022 12:07 PM
bash script: how to assign command ouput to a variable without executing it? bostonantifan Programming 1 02-13-2011 12:55 AM
How to assign a variable to the output value of a program BeyondSora Linux - Newbie 3 02-07-2011 01:46 AM
How do you assign a variable to be a variable file name in a directory? David_Elliott Programming 4 04-14-2009 11:19 AM
Shell script --cannot assign variable-- ralvez Programming 6 02-24-2006 05:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:49 PM.

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