Skip to main content
17 events
when toggle format what by license comment
Sep 4 at 9:48 comment added cas Or, and this may sound crazy, but why not join it properly in the first place and not have to bodge it up afterwards? A join function (even if it has to be named join_by or join_array or whatever to avoid conflicting with /usr/bin/join) is something that anyone doing shell scripting should have in their toolkit - it's something that will be used many times in many different situations. If not defined in .bashrc, then at least as a source-able standard library of common functions. Yes, I know, it's only bash. but even bash doesn't have to be written poorly.
Sep 4 at 9:20 comment added Sotto Voce Regarding printf "%s " "${array[@]}" joining the array elements into a string but leaving a trailing space (the separator character), sometimes it's faster and cleaner to do the printf join and follow it with a line that trims the trailing space. E.g., var="${var% }". With a comment, of course. Often whipping out three lines of imperfect join+trim+comment can be better than constructing and testing nine lines of function definition and a line calling it.
Sep 4 at 7:49 comment added cas @keelung one more thing: you should consider using typeset -p (AKA declare -p) instead of echo or printf for debugging. It's maybe not as pretty but it does a better job of telling you exactly what's in the array or variable.
Sep 4 at 7:12 comment added ilkkachu @cas, yep. Though Bash isn't too great at returning values from functions in any case, since e.g. j=$(foo ...) always launches a subshell, even if foo is a builtin...
Sep 4 at 7:00 comment added cas @ilkkachu [*] instead of [@] - yeah, that's true, that would work. I guess I think too much in perl-ish terms when it comes to working with arrays and lists. And bash really does need a proper join function. So does awk, for that matter. It's so essential a feature it should be built-in.
Sep 4 at 6:53 comment added ilkkachu The idea of "${array[@]}" is to have the array elements as distinct args, so that e.g. ls "${filenames[@]}" works for an arbitrary list of filenames, even with whitespace. But the shell can also join them into a string, just use "${array[*]}" instead.
Sep 4 at 6:50 comment added ilkkachu Why is my variable local in one 'while read' loop, but not in another seemingly similar loop? has information on making that whatever | while ... work.
Sep 4 at 4:18 comment added cas FYI, some essential reading about while read loops: Why is using a shell loop to process text considered bad practice? and Understanding "IFS= read -r line"
Sep 4 at 4:15 history edited cas CC BY-SA 4.0
added comments to join_by function
Sep 4 at 4:05 history edited cas CC BY-SA 4.0
added 146 characters in body
Sep 4 at 3:50 history edited cas CC BY-SA 4.0
deleted 8 characters in body
Sep 4 at 3:47 comment added Keelung Thanks for your explanation in detail! It's very clear and I'm understanding clearly!
Sep 4 at 3:44 vote accept Keelung
Sep 4 at 3:44 history edited cas CC BY-SA 4.0
added readarray example
Sep 4 at 3:33 history edited cas CC BY-SA 4.0
renamed join to join_by to avoid conflict with /usr/bin/join
Sep 4 at 3:25 history edited cas CC BY-SA 4.0
renamed join to join_by to avoid conflict with /usr/bin/join
Sep 4 at 3:20 history answered cas CC BY-SA 4.0