I want my script to be able to take an optional input,
e.g. currently my script is
#!/bin/bash
somecommand foo
but I would like it to say:
#!/bin/bash
somecommand [ if $1 exists, $1, else, foo ]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could use the default-value syntax:
The above will, as described in Bash Reference Manual – 3.5.3 Shell Parameter Expansion [emphasis mine]:
If you only want to substitute a default value if the parameter is unset (but not if it’s null, e.g. not if it’s an empty string), use this syntax instead:
Again from Bash Reference Manual – 3.5.3 Shell Parameter Expansion: