0

I have a shell script contains this condition

 if [ -z "${PEER_ADDRESS}" ]; then
    ##some code here
fi

I'm not able to pass PEER_ADDRESS variable when I run the script.

I tried the below but it always executed the if code

./script.sh PEER_ADDRESS="someString" 
./script.sh $1 ="someString" 
./script.sh "someString"
./script.sh $PEER_ADDRESS="someString" 
5
  • Please research other available solutions before asking. You want to run your third example in "what you tried", then the passed arg will be available in $1. See stackoverflow.com/questions/9732385, or unix.stackexchange.com/questions/32290 for a key=value notation being handled (it's just string processing; there's no magic "keyword args" function for passing things to Bash). Commented Jan 4, 2018 at 23:10
  • Note that your first attempt would work if you set the -k option first. Commented Jan 4, 2018 at 23:19
  • Hi @chepner,It didn't work also Commented Jan 4, 2018 at 23:21
  • What do you mean, "didn't work"? If you run ./script.sh "someString", then "$1" within that script will certainly expand to someString, and a question that claims otherwise should include a minimal reproducible example allowing others to see the problem for themselves. Commented Jan 5, 2018 at 0:28
  • I stated in my question very clearly how the script referenced the variable..like this "${PEER_ADDRESS}" , that's why this way didn't work for me. Commented Jan 5, 2018 at 4:26

1 Answer 1

3

The assignment precedes the command:

PEER_ADDRESS="someString" ./script.sh
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.