How can I pass a file argument to my bash script using a Terminal command in Linux? [duplicate]
It'll be easier (and more "proper", see below) if you just run your script as
myprogram /path/to/file
Then you can access the path within the script as $1
(for argument #1, similarly $2
is argument #2, etc.)
file="$1"
externalprogram "$file" [other parameters]
Or just
externalprogram "$1" [otherparameters]
If you want to extract the path from something like --file=/path/to/file
, that's usually done with the getopts
shell function. But that's more complicated than just referencing $1
, and besides, switches like --file=
are intended to be optional. I'm guessing your script requires a file name to be provided, so it doesn't make sense to pass it in an option.
you can use getopt to handle parameters in your bash script. there are not many explanations for getopt out there. here is an example:
#!/bin/sh
OPTIONS=$(getopt -o hf:gb -l help,file:,foo,bar -- "$@")
if [ $? -ne 0 ]; then
echo "getopt error"
exit 1
fi
eval set -- $OPTIONS
while true; do
case "$1" in
-h|--help) HELP=1 ;;
-f|--file) FILE="$2" ; shift ;;
-g|--foo) FOO=1 ;;
-b|--bar) BAR=1 ;;
--) shift ; break ;;
*) echo "unknown option: $1" ; exit 1 ;;
esac
shift
done
if [ $# -ne 0 ]; then
echo "unknown option(s): $@"
exit 1
fi
echo "help: $HELP"
echo "file: $FILE"
echo "foo: $FOO"
echo "bar: $BAR"
see also:
- the "canonical" example: http://software.frodo.looijaard.name/getopt/docs/getopt-parse.bash
- a blog post: http://www.missiondata.com/blog/system-administration/17/17/
man getopt
links dead. here from internet archive:
-
https://web.archive.org/web/20100325015445/http://software.frodo.looijaard.name/getopt/docs/getopt-parse.bash
-
https://web.archive.org/web/20130827004301/http://www.missiondata.com/blog/system-administration/17/17/
Bash supports a concept called "Positional Parameters". These positional parameters represent arguments that are specified on the command line when a Bash script is invoked.
Positional parameters are referred to by the names $0
, $1
, $2
... and so on. $0
is the name of the script itself, $1
is the first argument to the script, $2
the second, etc. $*
represents all of the positional parameters, except for $0
(i.e. starting with $1
).
An example:
#!/bin/bash
FILE="$1"
externalprogram "$FILE" <other-parameters>
Assuming you do as David Zaslavsky suggests, so that the first argument simply is the program to run (no option-parsing required), you're dealing with the question of how to pass arguments 2 and on to your external program. Here's a convenient way:
#!/bin/bash
ext_program="$1"
shift
"$ext_program" "$@"
The shift
will remove the first argument, renaming the rest ($2
becomes $1, and so on).
$@` refers to the arguments, as an array of words (it must be quoted!).
If you must have your --file
syntax (for example, if there's a default program to run, so the user doesn't necessarily have to supply one), just replace ext_program="$1"
with whatever parsing of $1
you need to do, perhaps using getopt or getopts.
If you want to roll your own, for just the one specific case, you could do something like this:
if [ "$#" -gt 0 -a "${1:0:6}" == "--file" ]; then
ext_program="${1:7}"
else
ext_program="default program"
fi