0

I have a shell script called displayArg.sh This is how I intend to run it-

./displayArg hello

and the output is entered arg is hello

The following is the script-

if [ $1 == "" ]; then
 default="Default"
 echo "no value is given. Output is $default"
else
 value=$?
 echo "entered arg is $value" #I know I am wrong in these 2 lines, but not sure how to fix it
fi

Kindly bear with me. I'm new to Shell scripting

1 Answer 1

2

You want:

value="$1"

($? is the status of the last command, which is 1 because the test command is what was executed last.)

Or you can simplify to:

if [ "$1" == "" ]
then
    echo "no value is given. Output is Default"
else
    echo "entered arg is $1"
fi

Note the quotes around "$1" in the test. If the string is empty, you get a syntax error. Your alternative with bash is to use a [[ $1 == "" ]] test.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.