printf and shell variable

Greetings,

In my script I have to process variables whose values
(happen to be file names) contains the '%' character

Since I have no control over how files are named,
I was wondering if there is a way I can get around
the situation which I am emulating through an example below:



# export filename=Hello%20World

# printf $filename
bash: printf: `W': invalid format character




Thanks,
Farid
Farid Hamjavar [ Di, 13 November 2007 00:16 ] [ ID #1868992 ]

Re: printf and shell variable

Farid Hamjavar wrote:
> Greetings,
>
> In my script I have to process variables whose values
> (happen to be file names) contains the '%' character
>
> Since I have no control over how files are named,
> I was wondering if there is a way I can get around
> the situation which I am emulating through an example below:
>
>
>
> # export filename=Hello%20World
>
> # printf $filename
> bash: printf: `W': invalid format character

Provide a format string for printf...

$ filename="Hello%20World"
$ printf "%s\n" "$filename"'
Hello%20World


Janis

>
>
>
>
> Thanks,
> Farid
>
>
>
Janis Papanagnou [ Di, 13 November 2007 00:44 ] [ ID #1868996 ]

Re: printf and shell variable

On 11/12/2007 5:16 PM, Farid Hamjavar wrote:
> Greetings,
>
> In my script I have to process variables whose values
> (happen to be file names) contains the '%' character
>
> Since I have no control over how files are named,
> I was wondering if there is a way I can get around
> the situation which I am emulating through an example below:
>
>
>
> # export filename=Hello%20World
>
> # printf $filename
> bash: printf: `W': invalid format character

Yes, use printf as the manual page tells you, i.e.:

printf <format> <text>

so in this case you'd do:

printf "%s\n" "$filename"

Regards,

Ed.
Ed Morton [ Di, 13 November 2007 00:55 ] [ ID #1869000 ]

Re: printf and shell variable

In news:Pine.LNX.4.64.0711121611080.17704 [at] chishio.swcp.com,
Farid Hamjavar <hamjavar [at] unm.edu> wrote:

> In my script I have to process variables whose values
> (happen to be file names) contains the '%' character
>
> Since I have no control over how files are named,
> I was wondering if there is a way I can get around
> the situation which I am emulating through an example below:
>
> # export filename=Hello%20World
> # printf $filename
> bash: printf: `W': invalid format character

Format the output as a string:
printf %s $filename
Gretch [ Di, 13 November 2007 00:56 ] [ ID #1869001 ]
Linux » comp.unix.shell » printf and shell variable

Vorheriges Thema: find can't redirect my output to a file
Nächstes Thema: Sed reqular expression