0

This question already has an answer here:

I would like to know how to add arguments to commands.

I would like it to be somewhat like this:

$ arg --test

'arg' would be the main command and '--test' would be the argument.

marked as duplicate by Jesse_b, Rui F Ribeiro, DisplayName, terdon shell-script Jun 4 at 11:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Would arg in your example be a shell script, C code, Python, or something else? – roaima Jun 2 at 13:43
  • 1
    Do you already know how to make arg so it can be called as in your example (but without --test)? – roaima Jun 2 at 13:44
  • Don't think that's necessarily a duplicate as it doesn't refer to getopt and friends to parse double-dash options. – roaima Jun 2 at 13:46
  • As is OP is basically just asking what a positional parameter is. If the question is modified to be more specific something like getopts could be useful. – Jesse_b Jun 2 at 13:54
  • +1 for "I won't just copy and paste the code you are supplying me with." – JL2210 Jun 2 at 14:16
0

For numbered arguments between 0 and 9 where x is the specific numbered argument, use this notation:

$x

For numbered arguments above that where x is the specific numbered argument, use this notation:

${x}

For a list of all arguments, use this:

${@}

Or this:

${*}

Note that when double-quoted, ${*} expands all arguments space-separated (usually, technically it's the first character of the variable $IFS (internal field separator)) into one string. ${@} expands into multiple strings when double-quoted.

Bracketing is not necessary when using ${@} or ${*}.

-1

This may be a duplicate to questions answered. However, please find a possible solution to your request as a very basic shell script below.

#!/bin/bash

for i in ${@} ; do
    case ${i} in
        --test)     echo "test option found"
                    ;;
        *)          echo "Usage: $0 [--test]"
                    exit 1
    esac
done
echo "$0 finished."

You may learn something more if you play around with options to this shell script on the command line. This may add to the first answer by JL2210 quite appropriately.

N.B. The line echo "Usage: $0 [--test]" aims to offer some help message and makes use of the convention to indicate optional arguments in square brackets.

Hope this helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.