Skip to main content
edited body
Source Link
Chris Davies
  • 4.1k
  • 1
  • 17
  • 29

You can't get at the unparsed command line; the shell receives the command line and parses it before starting your code. (Assuming there is a shell, of course. There doesn't necessarily need to be one, though.)

Example. Save this as args, and make it executable (chmod a+x args):

#!/bin/sh
echo "0=$0, #=$#, *=($*)"               # Program, number of args, list of args
printf '> %s\n' "$@"                    # Each arg in turn

You can invoke it in any of these ways and the resulting output will be the same

./args 'one two' three
a='one two'; ./args "$a" three          # Notice $a is quoted so remains one arg
./args 'one'" two" three                # Quotes are stripped by the shell
a=one b=; ./args $a"$a twotwo" $b three        # I really dislike unquoted variables
a=one; ./args "$a two" three            # Variations on a quoted theme
./args one\ two three                   # And more

The best you can hope for is to use "$@" and quote arguments as needed. If you have the non-standard getopt (not getopts) from the util‐linux credited to Frodo Looijaard you can use a couple of lines like this to get a quoted string:

line=$(getopt --shell sh '' -- "$@")    # Guess the command line
printf 'Command: %s %s\n' "$0" "${line# -- }"

But it's still not anywhere near a perfect reproduction of the original command line.

You can't get at the unparsed command line; the shell receives the command line and parses it before starting your code. (Assuming there is a shell, of course. There doesn't necessarily need to be one, though.)

Example. Save this as args, and make it executable (chmod a+x args):

#!/bin/sh
echo "0=$0, #=$#, *=($*)"               # Program, number of args, list of args
printf '> %s\n' "$@"                    # Each arg in turn

You can invoke it in any of these ways and the resulting output will be the same

./args 'one two' three
a='one two'; ./args "$a" three          # Notice $a is quoted so remains one arg
./args 'one'" two" three                # Quotes are stripped by the shell
a=one b=; ./args $a two $b three        # I really dislike unquoted variables
a=one; ./args "$a two" three            # Variations on a quoted theme
./args one\ two three                   # And more

The best you can hope for is to use "$@" and quote arguments as needed. If you have the non-standard getopt (not getopts) from the util‐linux credited to Frodo Looijaard you can use a couple of lines like this to get a quoted string:

line=$(getopt --shell sh '' -- "$@")    # Guess the command line
printf 'Command: %s %s\n' "$0" "${line# -- }"

But it's still not anywhere near a perfect reproduction of the original command line.

You can't get at the unparsed command line; the shell receives the command line and parses it before starting your code. (Assuming there is a shell, of course. There doesn't necessarily need to be one, though.)

Example. Save this as args, and make it executable (chmod a+x args):

#!/bin/sh
echo "0=$0, #=$#, *=($*)"               # Program, number of args, list of args
printf '> %s\n' "$@"                    # Each arg in turn

You can invoke it in any of these ways and the resulting output will be the same

./args 'one two' three
a='one two'; ./args "$a" three          # Notice $a is quoted so remains one arg
./args 'one'" two" three                # Quotes are stripped by the shell
a=one b=; ./args "$a two" $b three      # I really dislike unquoted variables
a=one; ./args "$a two" three            # Variations on a quoted theme
./args one\ two three                   # And more

The best you can hope for is to use "$@" and quote arguments as needed. If you have the non-standard getopt (not getopts) from the util‐linux credited to Frodo Looijaard you can use a couple of lines like this to get a quoted string:

line=$(getopt --shell sh '' -- "$@")    # Guess the command line
printf 'Command: %s %s\n' "$0" "${line# -- }"

But it's still not anywhere near a perfect reproduction of the original command line.

Source Link
Chris Davies
  • 4.1k
  • 1
  • 17
  • 29

You can't get at the unparsed command line; the shell receives the command line and parses it before starting your code. (Assuming there is a shell, of course. There doesn't necessarily need to be one, though.)

Example. Save this as args, and make it executable (chmod a+x args):

#!/bin/sh
echo "0=$0, #=$#, *=($*)"               # Program, number of args, list of args
printf '> %s\n' "$@"                    # Each arg in turn

You can invoke it in any of these ways and the resulting output will be the same

./args 'one two' three
a='one two'; ./args "$a" three          # Notice $a is quoted so remains one arg
./args 'one'" two" three                # Quotes are stripped by the shell
a=one b=; ./args $a two $b three        # I really dislike unquoted variables
a=one; ./args "$a two" three            # Variations on a quoted theme
./args one\ two three                   # And more

The best you can hope for is to use "$@" and quote arguments as needed. If you have the non-standard getopt (not getopts) from the util‐linux credited to Frodo Looijaard you can use a couple of lines like this to get a quoted string:

line=$(getopt --shell sh '' -- "$@")    # Guess the command line
printf 'Command: %s %s\n' "$0" "${line# -- }"

But it's still not anywhere near a perfect reproduction of the original command line.