Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .