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

  • Home
  • SEARCH
  • 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 3849978
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:56:19+00:00 2026-05-19T16:56:19+00:00

I am trying to allow strings that have whitespace in them to be counted

  • 0

I am trying to allow strings that have whitespace in them to be counted as whole strings, rather than being parsed. This is what I would like to have happen:

$ ksh program.ksh "what up"
File 'what up' not found.

And this is what I am getting:

$ ksh program.ksh "what up"
File 'what' not found.
File 'up' not found.

This is the code I’ve tried to make work:

wflag=false
cflag=false

while getopts "cw" opt; do
  case "$opt" in
    c)
      cflag=true
      ;;
    w)
      wflag=true
      ;;
    /?)
      ;;
  esac
done

The problem lies when 1) I try something above like ksh program.ksh "what up", and 2) when I try something like ksh program.ksh "-w -c". The second one is most confusing, as when I print out wflag and cflag they are both true. Shouldn’t "-w -c" be treated as a string here? It seems as though the kernel (is it the kernel doing the work here?) is parsing this, although that is exactly what I don’t want; I want it to be treated as a string with whitespace. I have read through WordSplitting and ((the article on Arguments, wouldn’t let me post it though)), and think I understand it. Clearly I must be missing something, or not really understanding it after all 😛

My instinct tells me there is something happening with getopts that automatically gets rid of any whitespace. I have tried $(opt) instead of "$opt", as well as with curly braces and/or single quotes (I’m pretty sure single quotes is not what would work though). Alas, no cigar.

Any help would be greatly appreciated, and thank you for reading through this wall of text.

  • 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-19T16:56:19+00:00Added an answer on May 19, 2026 at 4:56 pm

    The last item in your case statement should have a backslash instead of a slash. Let’s make a couple of other changes for the sake of diagnostics so you can see what’s happening.

    #!/bin/ksh
    wflag=false
    cflag=false
    
    # the first colon means suppress error messages
    # the colon after the x means that -x requires an argument
    while getopts ":cwx:" opt
    do
        echo "opt $opt"
        case "$opt" in
            c)
                cflag=true
                ;;
            w)
                wflag=true
                ;;
            x)
                echo "x is an option, $OPTARG is its argument"
                ;;
            \?)
                echo "invalid option"
                ;;
            *)
                echo "missing argument"
                ;;
        esac
    done
    echo "cflag $cflag"
    echo "wflag $wflag"
    

    Now let’s call it several ways:

    $ ./script.ksh -c
    opt [c]
    cflag true
    wflag false
    

    Fine.

    $ ./getoptstest.ksh -c -w
    opt [c]
    opt [w]
    cflag true
    wflag true
    

    Also fine.

    $ ./getoptstest.ksh -cw
    opt [c]
    opt [w]
    cflag true
    wflag true
    

    Still fine, but our answer is foreshadowed.

    $ ./getoptstest.ksh '-c -w'
    opt [c]
    opt [?]
    invalid option
    opt [?]
    invalid option
    opt [w]
    cflag true
    wflag true
    

    Now we’re getting somewhere. The Korn shell man page says:

    A leading : in optstring causes getopts to store the letter of
    an invalid option in OPTARG, and to set vname to ? for an
    unknown option
    and to : when a required option argument is miss‐
    ing. Otherwise, getopts prints an error message.

    As you can see from the example before this last one, you can run the options together as in -cw. When you pass an option like '-c -w', getopts sees it as -c -space -- and -w. So, in addition to two invalid options (-- and -space) it sees both -c and -w and that’s why both flags are getting set. Without the leading colon (which is the way the original script is posted in the question), you should be getting error messages like these:

    ./script.ksh: - : unknown option
    ./script.ksh: --: unknown option
    

    If you look closely, you’ll notice the telltale space and extra dash.

    It’s unfortunate that the man page doesn’t discuss the ability of getopts to process arguments passed separately or bunched together.

    I’m not sure if I understand what or if there’s a problem with your ksh program.ksh "what up" example.

    Try my modified script with these options and arguments to see how the rest of it works.

    ./script.ksh -x
    ./script.ksh -x foo
    ./script.ksh -z
    ./script.ksh -c bar -w
    

    That last one is tricky. When an argument that doesn’t begin with a dash is found and the preceding option doesn’t take an argument, the getopts stops processing and the rest of the arguments are left in place. You can use shift $(($OPTIND - 1)) after the while loop and then you’ll be able to access the remaining positional parameters in the usual way.

    Unfortunately, the shell builtin getopts doesn’t handle long options like --version. The external utility getopt does, but there can be some issues that have to be dealt with when it is used.

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

Sidebar

Related Questions

We are trying to build a SQL query builder that would allow users to
I am trying to allow very primitive customization based on being able to pass
ok I have two strings. (I use this for a language library system to
I am trying to set up a way to allow members to translate strings
I'm trying to write this custom addition class for very large integers, bigger than
So, I am trying to create a to_python converter that will allow me to
I have a command line script that generates some HTML that I am trying
I have the following possible strings that I need to turn into arrays so
I have a class ShipmentsCollection that inherits ObservableCollection which contains shipment objects (entities). This
Im trying to allow users to login to a website by verifying if they

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.