From 6702a32d9fdc0571570c28e9ba381c618d5a622c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20W=C3=BChrer?= Date: Fri, 25 Jul 2025 18:10:22 +0200 Subject: improve strjoin --- bin/strjoin | 13 ++++++++++--- man/man1/strjoin.1 | 43 ++++++++++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/bin/strjoin b/bin/strjoin index 83b2a9c..ca316c4 100755 --- a/bin/strjoin +++ b/bin/strjoin @@ -1,6 +1,13 @@ #!/bin/sh sep=$1 shift -printf '%s' "$1" -shift -printf "$sep"'%s' "$@" +if [ $# -gt 0 ] +then printf %s "$1" + shift + [ $# -gt 0 ] && printf -- "$sep"%s "$@" +else read -r a + printf '%s' "$a" + while read -r a + do printf -- "$sep"'%s' "$a" + done +fi diff --git a/man/man1/strjoin.1 b/man/man1/strjoin.1 index dd32f46..2e01e33 100644 --- a/man/man1/strjoin.1 +++ b/man/man1/strjoin.1 @@ -12,29 +12,38 @@ strjoin \- join words with a word is the command line equivalent of .BR python (1)'s .B str.join(...) -and java (1)'s +and +.BR java (1)'s .BR Stream.collect(Collectors.joining(...)) . -It is practically identical to -.B IFS=,;echo "$*" +For most purposes, +it is functionally almost identical to +.RS +.B strjoin(){ declare IFS=$1;shift;echo \(dq$*\(dq;} +.RE in .BR bash (1), -without having to set and reset the -.I IFS -variable. - +except the separator can be more than one character, +and also, lines to be joined can be read from +.BR stdin (3), +similar to +.BR paste (1)\ \-s , +except that with +.BR strjoin , +the separator can be more than one character. +.P The first parameter is the separator that goes between the words. All other parameters are the words. (They don't have to be single words, but they are treated as such.) -This is useful for constructing paths, -like for example -.BR java (1) -classpaths for -.BR jar (1) -manifest files, -but also tab-separated value records, -and tons of other things. - .SH EXAMPLES -.B strjoin , foo bar baz +.TP +.B strjoin\ ++\ foo\ bar\ baz +Prints \(lqfoo++bar++baz\(rq. +.TP +.BR printf\ \(aq%s\en\(aq\ foo\ bar\ baz\ |\ strjoin\ -- +Prints \(lqfoo--bar--baz\(rq, reading the words from stdin. +Useful for making lists of file names, for example. + +.SH SEE ALSO +.BR paste (1),\ tr (1) -- cgit v1.2.3