2

I'm trying to create a symbolic link to files with the following naming convention:

.$abc$namehere.zip;1

As can be seen, it has the following special characters in it: . $ ;

In my bash script I have the following lines of code:

FILE1=`find /mydir/ -name "\.\$abc\$*namehere*.zip*" | sort -rn | head -1`
ln -s $FILE1 .
echo "$FILE1"

When I issue the find command by itself on the command line, I get a result displayed on screen of the file that I am interested in. However, when it is placed in the script above, the symbolic link command returns:

ln: `./.': cannot overwrite directory

And the echo command returns a blank line instead of the file name.

May I get some help as to how I can fix this in my script so that I could create a symbolic link to these files with special characters?

1 Answer 1

5

Use $() instead of backticks:

FILE1=$(find /mydir/ -name "\.\$abc\$*namehere*.zip*" | sort -rn | head -1)
ln -s "$FILE1" .
echo "$FILE1"

The problem is that since you are enclosing your file's name in double quotes, bash is expanding it so this

\.\$abc\$*namehere*.zip*

is expanded to

\.namehere*.zip*

That's one of the many reasons why backticks are deprecated and $() should always be preferred.

4
  • <pre>ben@crystal:~$ echo echo "\.\$abc\$*namehere*.zip*" \.namehere*.zip* ben@crystal:~$ echo $(echo "\.\$abc\$*namehere*.zip*" ) \.$abc$*namehere*.zip* </pre> by the way you should double quote $file on ln, on echo it doesn't matter.
    – hildred
    Nov 25, 2013 at 6:55
  • @hildred yes indeed, I just copy pasted the OP's code and forgot to quote the var, thanks. I have no idea what the first part of your comment means though.
    – terdon
    Nov 25, 2013 at 23:34
  • It ate the whitespace, and the backquotes, which makes it hard to understand. To test the code I used echo to print the results of backtick and $() expansion of the double quoted string, and back ticks leave the backslash in front of the dot.
    – hildred
    Nov 26, 2013 at 0:28
  • bash should not be doing this thing. I think this is either a bug in bash or a deliberate departure from portability. The backslash is to be treated literally within backquoted command substitution - which should get eaten by "quotes" as you say - unless followed by $ ` `\` when it becomes a quote. This is the POSIX specified behavior anyway: pubs.opengroup.org/onlinepubs/9699919799/utilities/…
    – mikeserv
    Apr 28, 2014 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .