





















































Hi ,
Welcome to this week’s edition of ProgrammingPro!
In today’sExpert Insight, we bring you an excerpt from the recently published book, The Ultimate Linux Shell Scripting Guide, which explains how to use -u
and set -u
in shell scripts to identify uninitialized variables and prevent related errors during debugging.
News Highlights:Zed editor adds SSH remoting for faster remote workflows; IBM’s AI agents automate GitHub issue resolution; Java’s JEP 491 boosts concurrency with virtual thread improvements; and Tabnine’s Code Review Agent enforces custom coding standards.
My top 5 picks from today’s learning resources:
But there’s more, so dive right in.
Stay Awesome!
Divya Anne Selvaraj
Editor-in-Chief
🚀Join this 3-hour AI Workshop (worth $399) - FREE for ProgrammingPro readers to learn AI strategies & hacks to 10X work output and grow your business.
🗓️ Tomorrow | ⏱️ 10 AM EST
synchronized
keyword for mutual exclusion with Go’s sync.Mutex
and channels, showcasing adaptations needed.go vet
, staticcheck
, golangci-lint
, govulncheck
, and gosec
which support code analysis and vulnerability detection, and fuzz testing which helps find security flaws.Here’s an excerpt from “Chapter 21: Debugging Shell Scripts" in the book, The Ultimate Linux Shell Scripting Guide byDonald A. Tevault, published in October 2024.
As I said at the beginning of this chapter, in theUnderstanding Common Scripting Errors section, it’s sometimes desirable to define a variable in a script without assigning an initial value to it. But, sometimes it isn’t. You can track down uninitialized variables by appending a -u
to the end of your shebang line.
In bash
for example, you can use#!/bin/bash -u
, which will turn this feature on for the entire script. Or, you can place aset -u
command any place in the script where you’d like to start checking. For example, let’s look at the unassigned_var1.sh
script, which has variable checking turned off:
#!/bin/bash
echo "The uninitialized myvar, without setting -u, looks like this : " $myvar
echo
myvar=Donnie
echo "I've just initialized myvar."
echo "The value of myvar is: " $myvar
Here’s the output:
donnie@fedora:~$ ./unassigned_var1.sh
The uninitialized myvar, without setting -u, looks like this :
I've just initialized myvar.
The value of myvar is: Donnie
donnie@fedora:~$
As you see, without the-u
setting, the script runs to completion. It’s just that trying to echo the value of the uninitializedmyvar
just shows us a blank space. Next, let’s turn on variable checking by adding the-u
option, as you see in the unassigned_var2.sh
script:
#!/bin/bash -u
echo "The uninitialized myvar, without setting -u, looks like this : " $myvar
echo
myvar=Donnie
echo "I've just initialized myvar."
echo "The value of myvar is: " $myvar
Let’s see what this does:
donnie@fedora:~$ ./unassigned_var2.sh
./unassigned_var2.sh: line 2: myvar: unbound variable
donnie@fedora:~$
This time the script failed as soon as it saw the uninitialized variable.
You can set the-u
option anywhere in the script you like, by usingset -u
, as you see here in theunassigned_var3.sh
script:
#!/bin/bash
echo "The uninitialized myvar, without setting -u, looks like this : " $myvar
echo
echo "I'm now setting -u."
set -u
myvar=Donnie
echo "I've just initialized myvar."
echo "The value of myvar is: " $myvar
echo
echo "Let's now try another uninitialized variable."
echo "Here's the uninitialized " $myvar2
So now, I have one uninitialized variable at the top, on line 2. (Let’s just say that for whatever reason, I want this particular variable to be uninitialized.) I then turn on variable checking on line 5. Let’s see how this runs:
donnie@fedora:~$ ./unassigned_var3.sh
The uninitialized myvar, without setting -u, looks like this :
I'm now setting -u.
I've just initialized myvar.
The value of myvar is: Donnie
Let's now try another uninitialized variable.
./unassigned_var3.sh: line 11: myvar2: unbound variable
donnie@fedora:~$
Before I turn on variable checking, the uninitializedmyvar
just shows us a blank space. After I turn on variable checking, I initializedmyvar
with a value ofDonnie
, and it prints out normally. But, the uninitializedmyvar2
at the end crashes the script.
NOTE:
If you search the web for shell scripting security tutorials, you’ll find several that tell you to make either -u
orset -u
a permanent part of your scripts. The authors of these tutorials say that it enhances the security of your scripts, without giving any convincing explanation of why or how. Using-u
orset -u
is great for debugging, but it should only be used for just that—debugging! So, when you’re through debugging your scripts, be sure to remove the-u
or theset -u
before you place the script into production. Otherwise, your scripts could give you some rather unpredictable results.
Also, be aware that using-u
can also help you detect typos in your scripts. For example, if you define a variable asmynum=1
, but accidentally call back the value with$mymum
, the-u
will detect thatmymum
is an unset variable.
The Ultimate Linux Shell Scripting Guidewas published in October 2024. Packt library subscribers can continue reading the entire book for free or you can buy the bookhere!
That’s all for today.
We have an entire range of newsletters with focused content for tech pros. Subscribe to the ones you find the most usefulhere.
If your company is interested in reaching an audience of developers, software engineers, and tech decision makers, you may want toadvertise with us.