From: Russ P. on 7 May 2010 00:30 On May 6, 6:57 pm, Ben Finney <ben+u...(a)benfinney.id.au> wrote: > "Russ P." <russ.paie...(a)gmail.com> writes: > > On May 6, 5:17 pm, Seebs <usenet-nos...(a)seebs.net> wrote: > > > On 2010-05-07, Russ P. <russ.paie...(a)gmail.com> wrote: > > > > In other words, I need to replace slash with dash in bash. It just > > > > occurred to me that I am a poet and didn't even know it. > > > > $(echo "$foo" | tr / -) > > > > There's probably a bashism for this, but I wouldn't bother, usually. > > > Fabulous! Thanks. > > Be aware that this will impose the overhead of three extra processes for > every such substitutiontransformation you want to do. That overhead may be > acceptable for your use case, but you should still be aware of it. > > Doing it with a Bash parameter substitution will perform the > substitution all in the same process (zero extra processes). Either solution is perfectly fine for my problem, but yours gets the nod for not requiring an outside program. I have read an entire book on Bash and browsed the man page many times, but I never knew it had a that character-substitution capability built in. Russ P.
From: Teemu Likonen on 7 May 2010 07:12 * 2010-05-07 00:17 (UTC), Seebs wrote: > On 2010-05-07, Russ P. <russ.paielli(a)gmail.com> wrote: >> In other words, I need to replace slash with dash in bash. It just >> occurred to me that I am a poet and didn't even know it. > > $(echo "$foo" | tr / -) echo command may not be robust because it may interpret some part of $foo as options. For example, this doesn't print anything: string=-n echo "$string" Using printf or a "here string" is better: printf '%s\n' "$string" | tr / - tr / - <<< "$string" But I actually suggest using Bash's ${string//\//-} form.
From: Ed Morton on 7 May 2010 07:35
On 5/6/2010 7:04 PM, Russ P. wrote: > I am writing a bash script that needs to convert a directory name that > is several levels deep to another directory name with no > subdirectories. To do that, I will change all the slashes to dashes. > Is there a simple way to do this in bash? Thanks. > > In other words, I need to replace slash with dash in bash. It just > occurred to me that I am a poet and didn't even know it. > > Russ P. $ foo="a/b/c"; echo "${foo//\//-}" a-b-c Ed. |