LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Contents of Directory Displayed using Arithmetic Expression (https://www.linuxquestions.org/questions/programming-9/contents-of-directory-displayed-using-arithmetic-expression-616189/)

Mr. ameya sathe 01-25-2008 01:41 AM

Contents of Directory Displayed using Arithmetic Expression
 
Friday, January 25 2008, 12:58 IST
Dear Friends,
I executed the following command at bash command line.

Code:

echo Area: `expr $X \* $Y`

Output: Contents of the Present Working Directory similar to output of the command '
Code:

ls
' are displayed.

Question: How did this happen? :scratch:

Yours,
Ameya




Guttorm 01-25-2008 02:52 AM

Hi

You forgot to set $X and $Y?

expr \*
Outputs:
*

So:
echo Area: *

Outputs:
"Area: " followed by the content of the current directory.

Remember bash evaluates the * before it executes the echo command.

It's a bit strange but it does make sense.

Mr. ameya sathe 01-25-2008 10:16 AM

Quote:

Originally Posted by Guttorm (Post 3034682)
Hi

You forgot to set $X and $Y?


Yes; I have deliberately not set the value of $X & $Y. Their values are undefined.

Quote:

Originally Posted by Guttorm (Post 3034682)
expr \*
Outputs:
*

So:
echo Area: *

Outputs:
"Area: " followed by the content of the current directory.
.

Thank you for explaining the derivation.
A new question has arisen in my mind; how come the execution of these commands
Code:

$echo *
$echo **

give the listing of the directory contents.

Yours,
Ameya

osor 01-26-2008 11:04 AM

I think you need to read up on how shell-globbing works.


All times are GMT -5. The time now is 01:29 PM.