LDP/LDP/guide/docbook/abs-guide/param-sub.sh

44 lines
857 B
Bash
Raw Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
2004-04-28 12:08:07 +00:00
# param-sub.sh
# Whether a variable has been declared
#+ affects triggering of the default option
#+ even if the variable is null.
2001-07-10 14:25:50 +00:00
username0=
2005-02-09 20:53:43 +00:00
echo "username0 has been declared, but is set to null."
2001-09-04 13:27:31 +00:00
echo "username0 = ${username0-`whoami`}"
2001-07-10 14:25:50 +00:00
# Will not echo.
2005-02-09 20:53:43 +00:00
echo
echo username1 has not been declared.
2001-07-10 14:25:50 +00:00
echo "username1 = ${username1-`whoami`}"
# Will echo.
username2=
2005-02-09 20:53:43 +00:00
echo "username2 has been declared, but is set to null."
2001-09-04 13:27:31 +00:00
echo "username2 = ${username2:-`whoami`}"
2005-02-09 20:53:43 +00:00
# ^
2001-07-10 14:25:50 +00:00
# Will echo because of :- rather than just - in condition test.
2004-04-28 12:08:07 +00:00
# Compare to first instance, above.
2001-07-10 14:25:50 +00:00
2005-02-09 20:53:43 +00:00
2005-03-21 13:51:11 +00:00
#
2005-02-09 20:53:43 +00:00
2005-03-21 13:51:11 +00:00
# Once again:
2005-02-09 20:53:43 +00:00
variable=
# variable has been declared, but is set to null.
echo "${variable-0}" # (no output)
echo "${variable:-1}" # 1
# ^
unset variable
echo "${variable-2}" # 2
echo "${variable:-3}" # 3
2001-07-10 14:25:50 +00:00
exit 0