printf and shell variable

printf and shell variable

am 13.11.2007 00:16:38 von Farid Hamjavar

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

Re: printf and shell variable

am 13.11.2007 00:44:00 von Janis Papanagnou

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

Re: printf and shell variable

am 13.11.2007 00:55:23 von Ed Morton

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

so in this case you'd do:

printf "%s\n" "$filename"

Regards,

Ed.

Re: printf and shell variable

am 13.11.2007 00:56:24 von Gretch

In news:Pine.LNX.4.64.0711121611080.17704@chishio.swcp.com,
Farid Hamjavar 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