Well I'm not sure what you're using it for so I'm not sure what the best way to do it is. But substituting with sed comes to mind:
sed -r 's/([^ ]+)/\1,\1/g'
[^ ] is a character class that matches any single character other than a space. Putting + after it matches one or more non-spaces. Enclosing that in parentheses makes it a capture group. The group's opening parentheses is the first that appears in the pattern (in this case it is the only one) so it can be accessed as \1.
Passing the -r flag makes sed use extended regular expressions; I did that just so I wouldn't have to write + as \+. s tells sed to match a regular expression and perform a substitution. I chose /as my delimiter, as is common. The regular expression ([^ ]+) is matched, and in place of each matching string, \1,\1 is outputted, with each \1 replaced with what the parenthesized expression matched.
The g at the end causes it to do this as many times on each line as there are matches, rather than at most once per line.
Since you're actually replacing entire matches, you can actually forgo the explicit capture group altogether and just use the \0 group which is the entire regex match:
sed -r 's/[^ ]+/\0,\0/g'
Anyway, regarding the first problem, if you find that you do need to access positional parameters by number where the number is itself stored in a variable, you can do that by performing indirection with !, which bash permits in parameter expansion. The ith positional parameter is accessible as:
"${!i}"
For example, I made a script called from3:
#!/bin/bash
for ((i=3; i<=$#; ++i)); do
printf 'Argument %d is "%s".\n' "$i" "${!i}"
done
$ ./from3 first second third fourth fifth
Argument 3 is "third".
Argument 4 is "fourth".
Argument 5 is "fifth".
@αғsнιη I don't see any change there, only clarification. But my main point is that if you do take the time to improve a post (thanks!) and add a tag, please also remove any wrong tags. None of the tags there were actually relevant to the question. And just be careful when adding text, be sure it is correct. I know this isn't easy, and I'm cheating since English is my language, and it really is no big deal, but just you know, be careful :)
And I have no idea why I had commented twice there! Server glitch? Well, terdon glitch, or server glitch. Something screwed up anyway :)
@derobert Both. And my English is considerably better than my Greek. I was raised bilingual (Dad's American, Mom's Greek) but the last 2 years of highschool were in English, as were all of my subsequent academic studies, as well as work. And I read in English much, much more often than in Greek.
Well, I guess not 'considerably better'. I speak Greek like a native speaker. I speak English like a native speaker with a university degree, a book addiction and language geek leanings.
2
I'll mix them up, mind you, but that's the price of being multilingual.
English and Greek I can keep separate easily enough since I was raised speaking both natively. It's the ones I picked up as an adult that tend to bleed into each other and the mother tongues.
For instance, when I first started learning Spanish, I lost my French. Then, when I moved to France, I got my French back but at the detriment of my Spanish for a few months. After a while, they were all sorted again, but there were some confusing times there :)
@derobert They kinda do, actually. When starting a new language, you keep using the idioms from the one(s) you knew. Then, as you get more immersed in the new one, that one now creeps into what you write with the original ones. You need to reach a minimum level of proficiency in the new one as well and then they sort themselves out. At least, that happened to me with Perl and Python.
@derobert Ah, phew. That would have been embarrassing :)
@terdon Yeah, that definitely happens. But I say they don't really count because it takes, what, half a year to be nearly fully proficient in a computer language? A lot less work than a human language.
Hah, that is a complicated one. I used to do C++. Learning Java after having mastered C++ was ... errr... an exercise mainly in profanity. Just had to keep reminding myself, but it has garbage collection! which lets you forgive a lot of language flaws.
C++ is definitely complicated. A lot of times needlessly so. Unfortunately a lot of times because the standard committee seems to have run out of time, and just shipped whatever they had when it was time to hit the pub...
@FaheemMitha Yeah. Some of those "subtle" features are just WTFs. A lot of them, though, are useful and, once you get used to them, you really miss them in other C-like languages.
I have no idea how nice/terrible C++17 is. Someday maybe I'll work with it again. There are a lot more options for low level languages nowadays, e.g., Rust.
Number theory is just an example of something that is very complicated. Also algebraic geometry, and a lot of other branches. Some of which don't have proper names, and many of them have connections to each other.
But it's very different from a complicated language like C++.
Research math is quite big these days. And very scary for outsiders. Though practitioners seem to cope.
(Anyway, I ramble.)
Actually, I'd say programming languages and programming in general don't approach the kind of difficulty that math has. At least in programming you have a map and directions.
In math you're basically lost in a trackless jungle, without a compass.
@FaheemMitha Well one (maths) is research while the other (programming) is application. In research you try to break new ground, in programming (or engineering in general) you're trying to apply already known things to solve issues. Not a waterproof distinction, obviously.
@terdon I don't think that's ever happened to me. Of course, I don't know many languages. But I have spent time chafing over restrictions. Why can't I do this in this way, dammit?
C++ is particularly terrible in that respect. It's like wearing a steel corset or something.
@derobert Terdon speaks English like an educated native. It's hard for people to do that in multiple languages, other than professional linguists.
Though I've got a feeling that Gilles, for example, is equally articulate in both English and French. And possibly other languages.
I'm basically monolingual myself.
Combination of accident of birth and laziness/apathy.
Though the English of the Swedish people here is also very good.
Meaning Anna and Kusalananda. And I'm assuming English is not their native tongue.
And I've been in contact with a fair number of Germans who wrote excellent English. Better than most native speakers.
David Kastrup, for example.
@Kusalananda That's what I thought.
Actually, most Free Software people (I suppose the going term is "hacker") write at least serviceable English. Often it's good to very good. But I suppose most of them are highly educated and read a lot.
@FaheemMitha In my experience (of having worked in New Zealand and in the UK), the worst spellers are the natives. I think it has to do with learning the language as a 2nd language; you know the rules better, or rather, you don't improvise spelling and grammar as much.
@FaheemMitha Well, I know for a fact that I have Swedish friends that have English as their first language, and other Swedish friends that have Farsi as their first language. Nationality does not dictate what the first language is.