Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
2  
You can use "double-digit" parameters directly, just use braces: echo ${12} –  glenn jackman Dec 9 '13 at 18:19
    
Also, $# gives you the number of arguments. For example, with ./test.sh a b c, $# would evaluate to 3. –  DoxyLover Dec 9 '13 at 20:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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