1

How to declare the variable content when running the script?

I know that is possible to use $1 $2 $3 to set a variable when running the script ./script.sh this is sparta, but I did not found a way to set explicit.

Like ./script.sh URL=www.google.com COMMAND=wget

This is possible?

2

If you invoke it with the variable declarations before the command, bash will put those variables in the environment created for that command. After the command, the variables will no longer exist in the current shell:

$ cat ./script.sh
#!/bin/sh
echo URL is $URL
echo COMMAND is $COMMAND
$ ./script.sh URL=www.google.com COMMAND=wget
URL is
COMMAND is
$ URL=www.google.com COMMAND=wget ./script.sh
URL is www.google.com
COMMAND is wget
$ echo $URL,$COMMAND
,

Reference: http://www.gnu.org/software/bash/manual/bashref.html#Simple-Command-Expansion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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