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 4561678
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:06:32+00:00 2026-05-21T18:06:32+00:00

I have a script I wrote for switching to root or running a command

  • 0

I have a script I wrote for switching to root or running a command as root without a password. I edited my /etc/sudoers file so that my user [matt] has permission to run /bin/su with no password. This is my script “s” contents:

matt: ~ $ cat ~/bin/s
#!/bin/bash

[ "$1" != "" ] && c='-c'

sudo su $c "$*"

If there are no parameters [simply s], it basically calls sudo su which goes to root with no password. But if I put paramaters, the $c variable equals “-c”, which makes su execute a single command.

It works good, except for when I need to use spaces. For example:

matt: ~ $ touch file\ with\ spaces
matt: ~ $ s chown matt file\ with\ spaces 
chown: cannot access 'file': No such file or directory
chown: cannot access 'with': No such file or directory
chown: cannot access 'spaces': No such file or directory
matt: ~ $ s chown matt 'file with spaces'
chown: cannot access 'file': No such file or directory
chown: cannot access 'with': No such file or directory
chown: cannot access 'spaces': No such file or directory
matt: ~ $ s chown matt 'file\ with\ spaces'
matt: ~ $ 

How can I fix this?

Also, what’s the difference between $* and $@ ?

  • 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-21T18:06:32+00:00Added an answer on May 21, 2026 at 6:06 pm

    Ah, fun with quoting. Usually, the approach @John suggests will work for this: use "$@", and it won’t try to interpret (and get confused by) the spaces and other funny characters in your parameters. In this case, however, that won’t work because su’s -c option expects the entire command to be passed as a single parameter, and then it’ll start a new shell which parses the command (including getting confused by spaces and such). In order to avoid this, you actually need to re-quote the parameters within the string you’re going to pass to su -c. bash’s printf builtin can do this:

    #!/bin/bash
    
    if [ $# -gt 0 ]; then
        sudo su -c "$(printf "%q " "$@")"
    else
        sudo su
    fi
    

    Let me go over what’s happening here:

    1. You run a command like s chown matt file\ with\ spaces
    2. bash parses this into a list of words: “s” “chown” “matt” “file with spaces”. Note that at this point the escapes you typed have been removed (although they had their intended effect: making bash treat those spaces as part of a parameter, rather than separators between parameters).
    3. When bash parses the printf "%q " "$@" command in the script, it replaces "$@" with the arguments to the script, with parameter breaks intact. It’s equivalent to printf "%q " "chown" "matt" "file with spaces".
    4. printf interprets the format string “%q ” to mean “print each remaining parameter in quoted form, with a space after it”. It prints: “chown matt file\ with\ spaces “, essentially reconstructing the original command line (it has an extra space on the end, but this turns out not to be a problem).
    5. This is then passed to sudo as a parameter (since there are double-quotes around the $() construct, it’ll be treated as a single parameter to sudo). This is equivalent to running sudo su -c "chown matt file\ with\ spaces ".
    6. sudo runs su, and passes along the rest of the parameter list it got including the fully escaped command.
    7. su runs a shell, and it also passes along the rest of its parameter list.
    8. The shell executes the command it got as an argument to -c: chown matt file\ with\ spaces. In the normal course of parsing it, it’ll interpret the unescaped spaces as separators between parameters, and the escaped spaces as part of a parameter, and it’ll ignore the extra space at the end.
    9. The shell runs chown, with the parameters “matt” and “file with spaces”. This does what you expected.

    Isn’t bash parsing a hoot?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python script I recently wrote that I call using the command
I have a script that takes a table name and generates a control file
I have a script that parses the filenames of TV episodes (show.name.s01e02.avi for example),
I have a script that retrieves objects from a remote server through an Ajax
I have a script that checks responses from HTTP servers using the PEAR HTTP
I have a script for OS X 10.5 that focuses the Search box in
I have a script that renders graphs in gnuplot. The graphs all end up
I have a script that works fine on my test server (using IIS6). The
anyone have script or procedures to install SQL Server 2008 Express, set up the
I have a script which contacts a few sources and tell them the IP-address

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.