This is the mail archive of the
cygwin
mailing list for the Cygwin project.
Re: <<expr 100 * 100>> returns syntax error
- From: Adam Dinwoodie <adam at dinwoodie dot org>
- To: cygwin at cygwin dot com
- Date: Thu, 28 Jul 2016 14:14:17 +0100
- Subject: Re: <<expr 100 * 100>> returns syntax error
- Authentication-results: sourceware.org; auth=none
- References: <51777330c6d999e10af5937cf2e06ea6fe5@guerrillamail.com>
On Thu, Jul 28, 2016 at 12:55:43PM +0000, 45ownu+e60iporpd8lec@guerrillamail.com wrote:
> Steps to reproduce (I cannot update cygwin on this server, but it also happen on 2.5.2(0.297/5/3)):
> Main window:
> $ uname -r
> 1.7.25(0.270/5/3)
> $ expr 100 * 100
> 10000
>
> ---------------
> Other window:
> $ $ uname -r
> 1.7.25(0.270/5/3)
> $ HOST=myhostname
> $ echo 'whatever' > $HOST.txt
>
> ---------------
> Main window:
> $set -xv
> $expr 100 * 100
> + expr 100 myhostname.txt 100
> syntax error
>
> ------------------------------------------------
>
> When this happen I can only fix by rebooting the server, which is obviously unideal!
> Here my questions:
> 1) How can I reset cygwin to the initial behaviour without rebooting?
> 2) why does this happen? how to prevent this?
This is working as intended: '*' is a shell glob character, so Bash will
attempt to expand it.
In the first case, you're evidently in a directory that contains no
files or directories (excluding ones that start with a '.'), so the glob
doesn't expand and is left as-is. This means the `expr` command sees
the arguments as you typed them: `100 * 100`.
In the second case, you've created a file called myhostname.txt in this
directory, so the '*' glob will expand to the list of matching file
names, i.e. myhostname.txt. This means the `expr` command will see the
arguments after the glob expansion, i.e. `100 myhostname.txt 100`.
Run the following commands in a shell and you might see what's
happening:
mkdir tmp; cd tmp
echo *
touch file1
echo *
touch file2
echo *
touch file3
echo *
See also https://en.wikipedia.org/wiki/Glob_(programming)
You can disable this behaviour with `set -f`, or by escaping or quoting
the '*' character (i.e. `expr 100 \* 100` or `expr 100 '*' 100`).
HTH
Adam
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple