4

I have executed the following command sequence:

$ now=$(date)
$ echo _$now_
_
$ echo _ $now _
_ Mon Sep 22 09:53:44 IST 2014 _

Why is the output of _$now_ only _?

0

4 Answers 4

11

From man bash:

DEFINITIONS
       The following definitions are used throughout the rest of this document.
       blank  A space or tab.
       word   A sequence of characters considered as a single unit by the shell.
              Also known as a token.
       name   A word consisting only of alphanumeric characters and underscores, 
              and beginning with an alphabetic character or an underscore.  Also 
              referred to as an identifier.
...
PARAMETERS
       A parameter is an entity that stores values.  It can be a name, a number, 
       or one of the special characters listed below under Special Parameters.  
       A variable is a parameter denoted by a name.

A variable can only have alphabets, numbers and underscores. And so now_ is a valid variable name, and is interpreted as such.

You can delimit the variable name in different ways:

_"$now"_
_${now}_
_$now"_"
_$now'_'

Or any combination of the above.

10

because _ is part of the variable name in your echo _$now_

use echo \_$now\_ instead.

Also you can use that just in a linear command: echo _$(date)_

2
  • 1
    or use braces to separate the variable name from the surrounding text: echo _${now}_ Sep 22, 2014 at 12:27
  • @glennjackman that's covered in @muru's answer. thankx Sep 22, 2014 at 12:31
7

Bear with me for a moment, this requires a bit of explaining.

First of, why is output of _ $(date) _ is _ Mon Sep 22 03:30:34 MDT 2014 _ ? Because this literally tells echo to output _ first then output $(date) then _ . Spaces separate the variables for echo.

Now try echo _$(date), note no space between _ and $(date). In this case output will be _Mon Sep 22 03:32:40 MDT 2014 . What does this do? you tell echo to concatenate underscore with output of $(date).

Try same thing with _$PWD, which will concatenate your working directory with underscore. Now try echo $PWD_ . The output will be blank. Why? because PWD_ is non-existent environment variable, and as others have mentioned _ is valid character for an environment variable, e.g. $XDG_CURRENT_DESKTOP .

So why _$PWD_ would give _ ? Because you are telling echo to concatenate _ with output of non-existent environment variable. So _ is printed, but $PWD_ output is blank, so you literally see _ concatenated with that blank output.

2
  • 1
    Then, why print _? Sep 22, 2014 at 7:56
  • @ KasiyA I didn't read question first time around properly. See the edited answer Sep 22, 2014 at 9:41
1
_$now_ 

is interpreted as

_${now_}

which is, in your case, obviously the string concat of '_' and ''. Use therefore

_${now}_

instead. It is much more clear to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.