Draw a square with the rectangle tool. (You can uncheck the visibility any time.)
Draw your line. Set the Outline > Brush Type to Image and drag the Rectangle shape layer into the Brush Source well. Set the Spacing to 1.0% (click and drag on the % value downwards to 1% -- can't get to it with the slider.) The line will have a will look like it has a right angle edge on the ends. There's more to do:
At the top of the inspector column, select Stroke.
In the Stroke pane, look down the list about 2/3rds of the way and select: Align Brush Angle. The square "brush" will now align to the bezier curve.
Look back up the list three parameters and find Width Over Str... (you might want to drag out the width of the inspector to see the labels more clearly.) Dial down the disclosure triangle and click on the right most "keyframe" on the line. Under the graph (for more precision than dragging on it) use the Width Over Stroke slider to set the keyframe to 0.
You should be able to see the Width Over Stroke setup and the Align Brush Angle checked in the image above. On the right is the appearance of the line without masking.
I used this same basic technique to illustrate the snake's body in this illustration: http://rattlesnakeradio.com/
Instead of a square, I used a replicated circle to create a scale pattern (Color Over Stroke) and the Width Over Stroke to create the body contours.