Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: "printf %q $str" deletes spaces when in scripts. (Alternatives?)

Tags:

bash

printf

printf %q should quote a string. However, when executed into a script, it deletes the spaces.

This command:

printf %q "hello world"

outputs:

hello\ world

which is correct.

This script:

#!/bin/bash

str="hello world"
printf %q $str

outputs:

helloworld

which is wrong.

If such behavior is indeed expected, what alternative exists in a script for quoting a string containing any character in a way that it can be translated back to the original by a called program?

Thanks.

Software: GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)

EDITED: Solved, thanks.

like image 808
Eleno Avatar asked Jan 28 '12 11:01

Eleno


1 Answers

You should use:

printf %q "$str"

Example:

susam@nifty:~$ cat a.sh
#!/bin/bash

str="hello world"
printf %q "$str"
susam@nifty:~$ ./a.sh 
hello\ world

When you run printf %q $str, the shell expands it to:

printf %q hello world

So, the strings hello and world are supplied as two separate arguments to the printf command and it prints the two arguments side by side.

But when you run printf %q "$str", the shell expands it to:

printf %q "hello world"

In this case, the string hello world is supplied as a single argument to the printf command. This is what you want.

Here is something you can experiment with to play with these concepts:

susam@nifty:~$ showargs() { echo "COUNT: $#"; printf "ARG: %s\n" "$@"; }
susam@nifty:~$ showargs hello world
COUNT: 2
ARG: hello
ARG: world
susam@nifty:~$ showargs "hello world"
COUNT: 1
ARG: hello world
susam@nifty:~$ showargs "hello world" "bye world"
COUNT: 2
ARG: hello world
ARG: bye world
susam@nifty:~$ str="hello world"
susam@nifty:~$ showargs $str
COUNT: 2
ARG: hello
ARG: world
susam@nifty:~$ showargs "$str"
COUNT: 1
ARG: hello world
like image 145
Susam Pal Avatar answered Nov 15 '22 19:11

Susam Pal