From: Russ P. on
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.
From: Seebs on
On 2010-05-07, Russ P. <russ.paielli(a)gmail.com> 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.

$(echo "$foo" | tr / -)

There's probably a bashism for this, but I wouldn't bother, usually.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Russ P. on
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:
>
> > 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.
>
> $(echo "$foo" | tr / -)
>
> There's probably a bashism for this, but I wouldn't bother, usually.
>
> -s
> --
> Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nos...(a)seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Fabulous! Thanks.
From: Ben Finney on
"Russ P." <russ.paielli(a)gmail.com> writes:

> 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.

Since pedantry is a tradition in such forums: The U+002D HYPHEN-MINUS
('-') is not a dash.

> Is there a simple way to do this in bash? Thanks.

$ d="foo/rendered bacon fat/bar"

$ echo "${d//\//-}"
foo-rendered bacon fat-bar

The leaning toothpicks are explained in 'bash(1)', the “Parameter
Expansion” subsection:

${parameter/pattern/string}
Pattern substitution. The pattern is expanded to produce a
pattern just as in pathname expansion. Parameter is expanded and
the longest match of pattern against its value is replaced with
string. If pattern begins with /, all matches of pattern are
replaced with string. Normally only the first match is replaced.
[…]

--
\ “The Way to see by Faith is to shut the Eye of Reason.” |
`\ —Benjamin Franklin |
_o__) |
Ben Finney
From: Ben Finney on
"Russ P." <russ.paielli(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).

--
\ “The industrial system is profoundly dependent on commercial |
`\ television and could not exist in its present form without it.” |
_o__) —John Kenneth Galbraith, _The New Industrial State_, 1967 |
Ben Finney