Shell Scripting: If Variable Is Not Defined, Set Default Variable

If var is defined AND NOT EMPTY, use var, otherwise set a default variable under Bash. For e.g. my script needs a parameter for output variable. It can be text or html. I set it as follows in my script

ADVERTISEMENTS

output=$1 # either text or html

However, sometime user forget to pass the parameter to my shell script and my enter logic fails. So how do I set default value to text, if no parameter passed?

BASH, POSIX shell, and Korn (all versions) support the parameter expansion and testing. For e.g. if $1 is defined AND NOT EMPTY, use $1; otherwise, set to “text”, enter:

output=${1-text} 
echo $output

OR (see my comment below):

output=${1:-text} 
echo $output

Try another example at a shell prompt:
$ vech=Bus
$ echo ${vech-Car}
$ echo ${vech:-Car}
$ unset vech
$ echo ${vech-Car}
$ echo ${vech:-Car}

Finally, here is a sample script:

#!/bin/bash
output=${1-text}
echo "Setting output to $output..."

Now, run it as follows:
$ ./script.sh html
$ ./script.sh text
$ ./script.sh

You can also force to user to pass the parameter:

#!/bin/bash
output=${1-text}
[ $# -eq 0 ] && { echo "Usage: $0 format" ; exit 1; }
echo "Setting output to $output..."
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
8 comments… add one
  • Chris Jul 25, 2009 @ 13:15

    Wow, this is very helpful. Thanks!

  • Rahul Sonar Jul 26, 2009 @ 15:45

    thanks buddy, very useful post..

  • Susinthiran Jul 27, 2009 @ 6:59

    I didn’t know that we could easily use “output=${1-text}” to test & set a variable in shell..
    Thx

    B.R
    Susinthiran

  • Ronald Fischer Jul 27, 2009 @ 7:11

    It surprises me that this works (and, at least, on bash 3, it does). I have always used the form
    ${VAR:-VALUE} in such a case, not ${VAR-VALUE}, but it seems that both work. The man pages of bash describe, however, ONLY the variant with a colon, so I wonder whether omitting the colon just exploits an undocumented feature, which might be gone with the next version of bash. Or did I miss here something in ‘man bash’?

  • 🐧 nixCraft Jul 27, 2009 @ 7:59

    @Ronald,

    The original Bourne shell only supported above syntax and it works with all shells to keep portability. POSIX shells (KSH and BASH) offer a slight variant (as mentioned in bash man page):

    ${1:-text}

    I should have mentioned both syntax..

    HTH

  • Shantanu Oak Jul 27, 2009 @ 12:32

    I do completely agree with what Ronald has said. I strongly recommend using : – because it is more logical. A lot of people will like it since they are looking for equivalent of If – Then – Else logic and in this case : stands for “then” and – for “else”.

  • Jeff Jun 4, 2015 @ 4:03

    Thanks for the post! Found via Google and helped me out. There is apparently a difference between using the dash and using the dash with the colon. This page explains it quite well: http://www.tldp.org/LDP/abs/html/parameter-substitution.html

    Brief intro to when they differ:
    “${parameter-default} and ${parameter:-default} are almost equivalent. The extra : makes a difference only when parameter has been declared, but is null.”

  • Sadashiv Jul 30, 2015 @ 6:40

    Thanks a lot, Helped me to modify my script very less with this post.

    Thanks
    Sadashiv

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.