Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
<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 '13 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 '13 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 '13 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 at 13:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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