Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a udev rule that creates a symlink for serial devices (USB serial ports) using an expression like this: SYMLINK+="MyDevice_%n". The %n assigns the system device node number starting with 0. Is there a way to modify the numbering - instead of %n, perhaps %n+1? Math expressions don't work in a udev rule. I might be able to use a bash script to do the equivalent of echo $((%n+1)) but I'm not sure how to do this in a udev rule. Any helpful suggestions?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Use the udev rules PROGRAM field to execute a /bin/sh echo expression and capture the result using %c, like this...

PROGRAM="/bin/sh -c 'echo $((%n+1))'", SYMLINK+="MyDevice_%c"

The resulting symlink will have the equivalent of MyDevice_$((%n+1)) or MyDevice_1 if %n is zero. The $(()) construct is called "arithmetic expansion" and causes the contents to be evaluated as an integer expression. It's a syntax element of the shell.

share|improve this answer

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.