I have a shell script and when invoking it ./test
it asks for an input.I 've seen a quicker way just by writing at once ./test myInput
,how is that achievable?
Asked
Active
Viewed 2.0k times
0

jofel
- 26,758

Phil_Charly
- 191
1 Answers
5
You can access the command line arguments in your shell script with the special variables $1
, $2
until $9
. $0
is the name of your script.
If you need access more than 9 command line arguments, you can use the shift
command. Example: shift 2
renames $3
to $1
, $4
to $2
etc.
Please remember to put the arguments inside doubles quotes (e.g. "$1"
), otherwise you can get problems if they contain whitespaces.

jofel
- 26,758
echo ${12}
– glenn jackman Dec 09 '13 at 18:19$#
gives you the number of arguments. For example, with./test.sh a b c
,$#
would evaluate to 3. – DoxyLover Dec 09 '13 at 20:18echo "${12}"
or is the syntax sufficient asecho ${12}
. What is the difference? – crs1138 Jan 26 '18 at 13:59set -- one two three four five six seven eight nine ten eleven "12 twelve"; printf "%s\n" "${12}"; printf "%s\n" ${12}
– glenn jackman Jan 26 '18 at 14:38