I've read some other piping bash string manipulation questions but they seem to be specialized applications.
Essentially, is there a way to do the below simpler?
instead of
$ string='hello world'; string2="${string// /_}"; echo "${string2^^}"
HELLO_WORLD
something like
$ echo 'hello world' | $"{-// /_}" | "${ -^^}"
HELLO_WORLD
Edit I'm interested in staying within bash manipulations if possible to maintain speed (as opposed to sed/awk which have a tendency to greatly slow down my scripts)
Edit2: @jimmij
I like the second example and led me to making a function.
bash_m() { { read x; echo "${x// /_}"; } | { read x; echo "${x^^}"; }; }
echo hello world | bash_m
HELLO_WORLD
tr
manual, then the opposite is true because time of spawning the processes is negligible in comparison to time of string manipulation for whichsed
andawk
are dedicated. If string is extremely long, say the whole bash manual, then bash can just refuse to proceed altogether, because of some internal limitations.sed
,awk
,tr
or similar. Look at the gena2x answer, which I edited some time ago adding exactly this information: unix.stackexchange.com/questions/162221/… you may want to compare it with terdon answer to the same question where he gives time for short strings in which case process spawning takes most time. You can test it yourself and post result.read x; echo $x
is any better for performance? The syntax does not look any shorter or cleaner.x=${x// /_}; x=${x^^}
is a much more concise way to do the same thing as{read x; echo ${x...
. As far as performance goes, @jimmij has pointed out thattr
/sed
would be faster thanbash
, fork count being equal. Using a pipe always results in an extra process so the argument of saving a fork no longer applies. Thus, if are using pipes, just usesed
/tr
etc. If you can do it in bash, do so and skip thisread x; echo $x
nonsense.