echo date question


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users echo date question
# 1  
Old 08-16-2011
echo date question

Whats the difference between using date in these 2 methods? How exactly does the shell handle the first one different from the second one?

Code:
$ echo $date

$ echo $(date)
Tue Aug 16 03:10:25 EDT 2011

# 2  
Old 08-16-2011
Code:
% echo $date

% date=qwerty

% echo $date
qwerty

% date
Tue Aug 16 14:25:38

% echo `date`
Tue Aug 16 14:25:53

% echo $(date)
Tue Aug 16 14:25:58

% echo $(whoami)
yazu

# 3  
Old 08-16-2011
The first command prints the contents of the shell variable date. In your example it is empty.

The second command executes the command date in a subshell and then print the output of the date command. The result is identical to just running the date command by itself.
# 4  
Old 08-16-2011
Quote:
Originally Posted by yazu
Code:
% echo $date

% date=qwerty

% echo $date
qwerty

% date
Tue Aug 16 14:25:38

% echo `date`
Tue Aug 16 14:25:53

% echo $(date)
Tue Aug 16 14:25:58

% echo $(whoami)
yazu

Thank you Smilie. Remind me what these `` do please. What are they called?
# 5  
Old 08-16-2011
Quote:
Originally Posted by COKEDUDE
Thank you Smilie. Remind me what these `` do please. What are they called?
Backticks... they're essentialy the same. $() is the preferred method nowadays as it creates cleaner commands, though some very old shells do not support it.

As a rule of thumb use $() everytime when possible, unless there is a good reason forcing you to use backticks.

Example:

Code:
# Using $():
[root@atlas]# a=$(date)
[root@atlas]# echo $a
Tue Aug 16 09:35:38 CDT 2011

# Produces the same as ``
[root@atlas]# a=`date`
[root@atlas]# echo $a
Tue Aug 16 09:35:51 CDT 2011

# Nested $() looks clean and it's easy to read:
[root@atlas]# a=$(date $(echo "-u" $(echo "+%T")))
[root@atlas]# echo $a
14:41:48

# Whereas nested `` looks weird and sloppy:
[root@atlas]# a=`date \`echo "-u" \\\`echo "+%T"\\\`\``
[root@atlas]# echo $a
14:42:09

# 6  
Old 08-17-2011
Quote:
Originally Posted by verdepollo
As a rule of thumb use $() everytime when possible, unless there is a good reason forcing you to use backticks.
Get into the habit of using $( ) as it handles nesting commands where using backticks for nested commands will get ugly fast. Fugly even.

Consider this exercise in futility using the korn shell:
Code:
$ echo `echo hello`
hello
$ echo `echo hello  `echo there``  #  Trouble nesting
helloecho there
$ echo `echo hello  \`echo there\``  # Getting ugly
hello there
$ echo $(echo Hello $(echo there))  # Handles nesting
Hello there
$

This page shows some real-world examples: [Chapter 45] 45.31 Nested Command Substitution

Gary
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

UNIX shells script to echo out the date value

I appreciate if someone answer this question for my learning purpose: Given a filename structure of a COUNTRY CODE, file type, date (YYYYMMDD) and two digit attempt number with an extension of ".dat", write a UNIX shells script to echo out the date value. Example: ... (1 Reply)
Discussion started by: shumail
1 Replies

2. UNIX for Dummies Questions & Answers

echo * question

Hi all and tks in advance I am working my way through 'Learning the bash Shell'. I cannot be sure if I really understand why "echo *" (my double quotes) returns all the files in the current directory. I get the * ( wildcard) expansion of "*", and I think I understand that "echo" returns it's... (5 Replies)
Discussion started by: mdeh
5 Replies

3. Shell Programming and Scripting

Echo date variable from data input to a script

Hi, I'm trying to make a script which you type the year, select the month and day and then create the date in the format 2010-12-7. #!/bin/bash dia () { echo " Seleccione el dia:" select file in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Salir do... (6 Replies)
Discussion started by: iga3725
6 Replies

4. Shell Programming and Scripting

With that logic this echoes "echo". Question about echo!

echo `echo ` doesn't echoes anything. And it's logic. But echo `echo `echo ` ` does echoes "echo". What's the logic of it? the `echo `echo ` inside of the whole (first) echo, echoes nothing, so the first echo have to echo nothing but echoes "echo" (too much echoing :P):o (2 Replies)
Discussion started by: hakermania
2 Replies

5. Shell Programming and Scripting

Echo question

How can I get the following to prompt me for new values for the dacsrtrspans? example I want to change the span #, sinktermmod, sinktermport, srctermdev and srctermport to new values. old: ADD DACSRTRSPAN-1-840 SINKTERMMODULE=8 SINKTERMPORT=94 SRCTERMDEV=1 SRCTERMPORT=1 SPANTYPE=T1_2... (0 Replies)
Discussion started by: tadzooks
0 Replies

6. OS X (Apple)

Question about cat and echo

Hello, I am trying to send text to a USB to serial adaptor and then to an external speech synthesizer. I tried using the cat and echo commands with no luck. I have gotten some audio output from my synthesizer using Kermit a terminal emulator, so I am pretty sure my synthesizer and my USB to serial... (1 Reply)
Discussion started by: jamesapp
1 Replies

7. Shell Programming and Scripting

An echo question

Why do get 0 when i enter " echo $? " (1 Reply)
Discussion started by: JamieMurry
1 Replies

8. UNIX for Dummies Questions & Answers

Simple 'echo' question

Hi, I would like to output the identical line to 2 text files, ie output='blah' echo $output > test1.txt echo $output > test2.txt Is there a way I could do that output with ONE command, ie output='blah' echo $output > test1.txt & test2.txt (I know that doesn't work) Thanks for any... (1 Reply)
Discussion started by: msb65
1 Replies

9. Shell Programming and Scripting

question about echo $$

hi all i have script echo $$ >tmp i understand thatecho $$ display some process id which is unique is that correct ? and my othere question is what is the maximum length that process-id can be ? the reason that i'm asking this is because i have a program which need to read the number of the... (1 Reply)
Discussion started by: naamas03
1 Replies

10. UNIX for Dummies Questions & Answers

echo date

Hi, How to display date using echo command. I tried "echo date" , it prints "date" not actual date only date command prints actual date. Thanks (4 Replies)
Discussion started by: avadhani
4 Replies
Login or Register to Ask a Question