1

Possible Duplicate:
How to tell if a string is not defined in a bash shell script?
What’s the best way to check that environment variables are set in Unix shellscript

I'm trying my hand at Unix shell scripting for the first time and in a script I'm working on now, I want 2 of the 3 arguments to my script to be optional. However, if the user doesn't enter the arguments, I need to use default values. How can I properly say something that basically means:

$argument = $2
if($argument  == "")
    $argument = "defaultVal"

Thanks!

5

2 Answers 2

2

try:

argument=${2:-default value}
Sign up to request clarification or add additional context in comments.

1 Comment

Both these answers seem to work great - I just accepted the first one I got working! Thanks to both of you for your quick replies!
1

Try this:

if [ "x$2" == "x" ]; then
    $argument="default";
else
    $argument=$2;
fi

3 Comments

Is that trick still necessary? if [ "$2" = "" ]; works fine, in bash and dash at least.
@chepner I had problems with the "" being recognized as an empty token producing a syntax error, so I'd say yes.
Both these answers seem to work great - I just accepted the first one I got working! Thanks to both of you for your quick replies!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.