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.

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 making a script for monitoring some user processes with Upstart but, since its for the company I work for, they asked me to do it generic...how's so? Well, the number of processes being monitored could vary, as in number as in name so I need to create it so a user can input "n" number of processes and give the shell its pathname's so I can store them in variables and pass Upstart a script with them so my question is: how can I create as many variables as needed?

Here's my script so far…

./script input



while read LINE
echo "ruta -> [rutaServ$i]"
i=`expr $i + 1`
done < $FILENAME

As you can see I am taking the pathname provisionally from a plain text file with nothing more than /home/user lines in it but, when I run this script I get the following error [rutaServ1]=/home/user: The file or directory doesn't exists

What is the correct way of doing this?

share|improve this question
up vote 3 down vote accepted

It looks like your array syntax is off just a bit. Also, there's no need for the index variable; you can use the += operator to append to an array.



while read LINE
echo "ruta -> $LINE"
done < "$FILENAME"

bash v4 has a new command, mapfile (or readarray) to read the contents of a file into an array all at once:

mapfile rutaServ < "$FILENAME"

To access values from the array:

for x in "${rutaserv[@]}"; do
    echo "$x"
share|improve this answer
Ok, tried this and worked but after the loop if I try something like echo $LINE I get nothing in the output. How can I access the values of the array? – Kronos May 13 '13 at 16:58
great! that worked! Now I have a doubt, what does the "@" stands for in the index array? – Kronos May 13 '13 at 17:24
Think of it as "a for all": it expands the array to a list of words, one word per element in the array. It is quoted so that any element that contains whitespace will be treated as a single word. – chepner May 13 '13 at 17:38

Could the script be called like script.sh process1 process2? Then you could refer to the arguments with $@:

# set process1 process2 # set arguments for testing

for x in "$@"; do 
    echo "ruta -> $x"

[rutaServ$i]=$LINE should be rutaServ[$i]=$LINE or rutaServ+=("$LINE").

share|improve this answer
I tried your suggestion for the index before I try the arguments option because, I don't know how many processes will be, it could take long to write all arguments. So...here's my new code where in the end I test the variables... while read LINE do rutaServ[$i]=$LINE echo "ruta -> $rutaServ$i" done < "$FILENAME" i=1 while [ $i -le 3 ] do echo $rutaServ$i i=expr $i + 1` done` Now it works as saving the variables but when printing, it appends a number to the path: /home/user/ruta31 /home/user/ruta32 /home/user/ruta33 – Kronos May 13 '13 at 17:12
You can access array elements like echo "${rutaServ[$i]}". – user495470 May 13 '13 at 17:23

Your Answer


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.