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 debugging a shell script (which I didn't write) that contains this loop:

read line < "$pid_file"
for p in $line ; do
  [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
done

Can someone point me to documentation on using (what I'm assuming) is a regular expression within a string variable construct, i.e. the "${p//[0-9]/}". This topic isn't easy to narrow down in Google.

share|improve this question
    
Try running that after cd /proc with a $pid_file that contains * 1\ 2=2 3 –  Stéphane Chazelas Jan 21 '14 at 7:35

1 Answer 1

up vote 1 down vote accepted

This is a form of parameter expansion (i.e. variable expansion) with a text transformation of the value of the variable. The value of the variable p undergoes the replacement of the pattern [0-9] to the empty string wherever it occurs — in other words, "${p//[0-9]/}" is the value of p without its digit characters.

In the bash documentation, you'll find it under ${parameter/pattern/string}. This form replaces the first occurrence of the specified pattern with the specified string. If the first slash is doubled, all occurrences are replaced. The pattern is a glob, i.e. the same wildcard patterns as in filename matching.

share|improve this answer
    
Perfect, that link is exactly what I needed. THANK YOU! –  Alan Jan 21 '14 at 0:04
    
So basically, they're testing that p is numeric, and that its /proc/ directory exists. Correct? –  Alan Jan 21 '14 at 0:09
    
@Alan Yes, that's it. –  Gilles Jan 21 '14 at 0:10

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.