I have a full names that have been read into arrays. I am trying to create a file using only the last name; the last name might have white spaces that should be replaced by underscores. My thought is to create a string of the file name and then create the file. I have already taken care of the cases with only one last name. I am having trouble with the last names with white spaces. This is what I have so far:
if [ "${#name_arr[@]}" -eq 2 ]; then
for i in "${name_arr[@]:1}";do # :1 because first element is their first name
last_name=$i
done
echo $last_name
else
for i in "${name_arr[@]:1}";do
last_name=${last_name}_${i}
done
echo $last_name
fi
The output of this concatenates all of the names with underscores. So instead of:
Doe
Austen
Vaughn_Williams
Doe
It is echoing:
Doe
Austen
Austen_Vaughn_Williams
Doe
if [ "${#name_arr[@]}" -eq 2 ](note the#)?_Vaughn_Williamsand notAusten_Vaughn_Williams. It will start with an '_' based on your code. Are you sure you are posting the correct output?long_last_name, then it does output_Vaughn_Williams.