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'm trying to write a script that would add a prefix to a variable, but I can't seem to get it added. I've tried using ++1 and a few others but I'm not sure how to do this. Here's the bash

HOSTNAME=TEST-HOST

for i in {1..5};
do COMMAND IS RUN HERE AND INCLUDES $HOSTNAME;
done
share|improve this question
1  
Your code only shows a simple use of a variable and that should work if $HOSTNAME is not in single quotes. Where do you apply a prefix? –  Michael Suelmann Nov 3 '13 at 9:34
    
I'd like to add an incrementing number to the end of the $HOSTNAME variable. i.e 01, 02, 03 etc; –  RomeNYRR Nov 3 '13 at 9:36
2  
Do you mean like e.g. ping ${HOSTNAME}${i}? –  Daniel Beck Nov 3 '13 at 9:43
add comment

1 Answer 1

up vote 2 down vote accepted

If you need TEST-HOST1 you can just use $HOSTNAME$i inside your loop.

If you need TEST-HOST01 you can use $(printf "%s%02d" $HOSTNAME $i)

share|improve this answer
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.