Reputation: 675
My bash script bash.sh only contains one line
echo "${abc:=123}"
I learned that :=
is used to assign default values. So when I run bash.sh abc=abc
, I expect the output to be abc
.
However, the output is still 123
.
Why is that? Am I call the script in the wrong way? Thanks.
Upvotes: 0
Views: 1220
Reputation: 123410
You are passing a parameter and expecting to see it in an environment variable.
If you want to set an environment variable, you can do that before the script name:
$ cat foo
#!/bin/bash
echo "${abc:=123}"
$ ./foo
123
$ abc=hello ./foo
hello
Upvotes: 1
Reputation: 728
Bash positional arguments are set to $1
, $2
, etc. Change your script to:
abc=$1
echo "${abc:=123}"
this will make it so if the variable abc
is unset the default value is echoed but if another value is passed on the command line abc
will be set to that value.
Upvotes: 1