replace space characters in bash variable - Help

This is a discussion on replace space characters in bash variable - Help ; I have a script associated with some print queues (using tea4cups). I use the jobname to create a file that contains the results of the script. I build the jobname by: jobtitle=$TEAJOBID~`basename $TEATITLE` which will produce something like: 56~jobname.pdf (The ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: replace space characters in bash variable

  1. replace space characters in bash variable

    I have a script associated with some print queues (using tea4cups).
    I use the jobname to create a file that contains the results of the
    script.
    I build the jobname by:

    jobtitle=$TEAJOBID~`basename $TEATITLE`

    which will produce something like:

    56~jobname.pdf

    (The job number, plus the name of the job less path.)
    The problem is if jobname contains a space. Then I might get:

    56~job name.pdf

    which is inconvenient. Job names may contain any number of
    spaces, and this is especially a problem if it is a leading or trailing
    space, as may not be evident when viewing a job list.

    I tried replacing any spaces by:

    jobname=${jobname// /-}

    Oddly, this works when I type it directly in a command shell, but does
    NOT work when it executes as part of the script. Any suggestion as to
    the problem (or a better way to replace spaces?)

  2. Re: replace space characters in bash variable

    On Wed, 07 Nov 2007, in the Usenet newsgroup comp.os.linux.help, in article
    , Gary Carroll wrote:

    >I have a script associated with some print queues (using tea4cups).
    >I use the jobname to create a file that contains the results of the
    >script.
    >I build the jobname by:
    >
    >jobtitle=$TEAJOBID~`basename $TEATITLE`


    OK

    >The problem is if jobname contains a space. Then I might get:
    >
    >56~job name.pdf
    >
    >which is inconvenient. Job names may contain any number of
    >spaces, and this is especially a problem if it is a leading or trailing
    >space, as may not be evident when viewing a job list.


    jobtitle=$TEAJOBID~`basename $TEATITLE | tr -d ' '` or
    jobtitle=$TEAJOBID~`basename $TEATITLE | tr ' ' '_'` or some other
    printable character that wouldn't be confused.

    >I tried replacing any spaces by:
    >
    >jobname=${jobname// /-}
    >
    >Oddly, this works when I type it directly in a command shell, but does
    >NOT work when it executes as part of the script.


    Not enough details - shell, shebang line - something different about
    the environment. The newsgroup you are posting to 'comp.os.linux.help'
    is a "bogus" group not carried on that many news servers. You'd get
    better luck over in 'comp.unix.shell'. Have you tried there?

    Old guy

+ Reply to Thread