Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7195829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:39:30+00:00 2026-05-28T20:39:30+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:39:30+00:00Added an answer on May 28, 2026 at 8:39 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this regex on a forum: /\+?\)\)(=.+?,.+?)?\](.+?)\[\/quote\](?!((.*?)\[\/quote\]))/s The regex should be able to
I'm working on a dispatching script. It takes a string with a command, does
If for example I should not use standard library functions like printf() , putchar()
#include<stdio.h> int main() { int a,b; a=a+b; printf(%d,a); return 0; } what should be
All of my codes fail. They should print he, hello and 5 \n 3,
I'm parsing an HTML file into a well-formed XML document using NekoHTML parser. However
I have a small piece of c code which should run an awk command
I want to write a small program that should print something like testing CPU...
How to print multiply flowdocumets in a batch? Following code should print different documents
So far I have managed to write some code that should print the source

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.