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.
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.
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 ${*}
.
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.
arg
in your example be a shell script, C code, Python, or something else? – Chris Davies Jun 02 '19 at 13:43arg
so it can be called as in your example (but without--test
)? – Chris Davies Jun 02 '19 at 13:44getopt
and friends to parse double-dash options. – Chris Davies Jun 02 '19 at 13:46