Search code examples
ksh

ksh - why presence of the "\" file in current working directory changes behavior of variable processing


I had an issue with third party ksh script. Found out, that it was failing because of file named "\" in user home directory.

Here is a simple testcase:

$ mkdir -p ~/dir1 && cd ~/dir1 && touch '\' && x="\* a" && echo $x
\ a
$ mkdir -p ~/dir2 && cd ~/dir2 && x="\* a" && echo $x
\* a

The question is, why the presence of "\" file in a working directory changes the result. Is this expected?

Thanks.

T.


Solution

  • Looks like the expected behaviour.

    If you want the same behaviour in both cases, either use set -o noglob inside your script, or run the script with the -f option to disable file name substitution.

    The default is that the * is a special character when interpolating so will match whatever file exists (in your case dir1 will contain only one real file with the name of the backslash character.)

    The second directory dir2 has no real files so ksh just shows the pattern exactly as you typed it.