rabotalius
rabotalius

Reputation: 1568

Variable with asterisk

I need to find some image files with$tempThumb, but for some reason the following code doesn't work in my bash script, although I tried the same in terminal and it did work

randomStr=$(date | md5sum | tr -d ' -')
tempThumb=$(echo "$randomStr _ tempthumb" | tr -d ' ')
ls /dev/shm/$tempThumb*.jpg | sort -t '-' -n -k 2 | tr '\n' ' '

Upvotes: 0

Views: 170

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753585

You can simplify the second line to:

tempThumb="${randomStr}_tempthumb"

You could even use:

tempThumb=$(date | md5sum | tr -d ' -')_tempthumb

to replace the first two lines.

That doesn't explain the problem, though. Is /bin/sh a link to bash or to some other shell? [Answer: it is a link to dash.]

Does the script have a shebang line? Does it list #!/bin/bash or #!/bin/sh or something else? (On Mac OS X, both /bin/bash and /bin/sh work as desired on the script:

tt="cs"
ls $HOME/soq/$tt*.c

listing 3 files that start with cs and end .c in $HOME/soq.)

[This] actually solved my problem — ls /dev/shm/${tempThumb}*.jpg

Intriguing — glad to have helped. I just downloaded and built dash 0.5.7-3 from Debian and built it on Mac OS X 10.8.4, and then used it on the two-line script above and it worked fine as both dash and sh. I think that using ${tempThumb} is a good idea, but I don't have a good explanation for why you were running into the problem. Maybe you have a non-default setting for the shell?

Upvotes: 1

Related Questions