Why ShellCheck is the tool you’re missing


ShellCheck is a code verifier that spots bugs, incompatibilities, and other problems in shell scripts. It’s valuable because few similar tools exist, and shell programming can be particularly flaky, with portability concerns and different syntax changes introduced over time.

I unleashed ShellCheck on my own scripts—and a few third-party ones—and, unsurprisingly, it caught quite a few problems. So I’d recommend you do the same!

What is ShellCheck?

A shell script static analysis tool (and website)

ShellCheck bills itself as a “shell script static analysis tool,” meaning that you run it in a terminal, and it tells you how good your shell scripts are. Specifically, it carries out its job just by looking at the source code of your scripts, not by actually running them.

The program will warn you about various problems in your code, including:

  • Syntax errors and problems
  • Common semantic errors
  • Incompatibilities
  • Style issues that may not cause problems, but can lead to less readable code

For example, here’s a tiny shell script, repeat.sh, that prints its first argument:

#!/bin/bash
echo $1

You might think that there’s little to go wrong with such a simple script, but ShellCheck will correct that misconception:

Output from the shellcheck program with a message that reads "Double quote to prevent globbing and word splitting."

How do you use it?

Command-line and web versions are available

For a start, ShellCheck has an excellent web app which mirrors the latest version of the code and lets you try it out without installing a thing. I wouldn’t recommend using it long-term because it’s much less convenient, but it’s a great way to test the software and see what it does.

Because your local version may not be the very latest, you might see differences between it and the web version. Make sure you use your package manager to keep your local version up-to-date.

If you decide to try ShellCheck for real, start with the installation instructions for your platform. On Debian/Ubuntu, it’s sudo apt install shellcheck; on Fedora, use dnf install ShellCheck. If you have Homebrew, run brew install shellcheck.

Once it’s installed, you can run the tool with the command shellcheck. Without any arguments, you’ll get usage information:

ShellCheck’s usage shows a number of options like --color, --include, and --shell.

Now try running shellcheck on a script file to see some useful results, e.g.:

shellcheck script.sh

If your script has any problems in it, you should notice that ShellCheck provides several bits of useful information. For a start, it will describe each problem it encounters, with the file name, line number, and specific position in the line, e.g.:

ShellCheck displaying advice to use double quotes around a variable to avoid globbing.

“SC2086” is a unique error code that ShellCheck uses for reference. The full list of over 500 codes is available on ShellCheck’s wiki.

Alongside the specific error message, ShellCheck offers a suggested alternative, plus a set of reference links at the end to each unique issue. These pages give much more detail, including why people might make a particular mistake, and whether there are exceptions to the rule.

ShellCheck has a few options that you can use to tweak how it operates. The --format=json option will give you output in a more structured format, should you want to process it further. With --severity=error, you’ll only get errors reported; style, info, and warning types will be ignored. And the --include= and --exclude= options can be followed by a comma-separated list of specific codes to refine the scope. You can omit the leading “SC” if you want.

What kinds of problems can ShellCheck uncover?

The best way to find out is to try it

Many things can go wrong with shell scripts. Shell script is almost always interpreted, and the language is usually forgiving, with loose (or nonexistent) typing. There are many syntax differences between versions, and alternative syntaxes are maintained to provide backward compatibility. Moreover, there are different shells—like Bash and Zsh—that are more or less interchangeable, yet still retain some differences.

ShellCheck aims to uncover all these problems, so it’s pretty comprehensive. Here’s an example from one of my own shell scripts:

ShellCheck suggesting the use of $() notation instead of backticks.

This highlights a very common problem: the use of legacy syntax. While the backtick syntax (“) works for command substitution, it’s not ideal; for one thing, it doesn’t allow commands to be nested. The more modern $(...) variant is POSIX-compliant, so there’s no good reason to avoid it.

ShellCheck also caught my sloppy use of $0 without quotes. As the info message explains, this helps to prevent expansions that I’m probably not expecting. These two problems are both stylistic, so they’re not too much of a concern, but the next finding is more serious:

ShellCheck explaining that the order of redirections in a command matters.

This is an actual warning—note the slight color difference—because it reveals a fundamental misunderstanding: the order of 2>&1 and other redirections is important. My bad version will still show errors on the command line, even though I’m redirecting stdout to /dev/null, because of the order.

This is the exact kind of problem that is difficult to spot: it shouldn’t come up during normal program execution, and it could even be what I intended, it’s just unlikely. Thankfully, ShellCheck is clever enough to warn me about this technically correct but weird code!

To make myself feel a bit better, I checked some shell scripts that proper, public, open-source projects use. Fortunately for me, I’m not the only one who produces sloppy shell scripts! The report for the excellent bashblog tool included many problems. It’s a much bigger script, of course, but it helps demonstrate the issues that ShellCheck can catch.


The quality of your shell scripts matters too

Because shell scripts are often private or written to perform a throwaway task, it’s easy to get sloppy and miss important problems, even bugs. But we should treat shell scripts with just as much care as other types of programs.

Shell scripts can mutate over time, serving purposes they weren’t originally intended for. You never know where your code will end up, or who may one day use it, so make sure it’s as bulletproof as you can, while that’s easy to do.



Source link

Leave a Reply

Subscribe to Our Newsletter

Get our latest articles delivered straight to your inbox. No spam, we promise.

Recent Reviews


The first time I encountered mesh Wi-Fi was when I went to university. One Wi-Fi password, but no matter where you roamed on campus you’ll stay connected. I’ve always thought of mesh networks as enterprise technology that you need an IT department to handle, but then router makers figured out how to make mesh easy enough for mere mortals.

Now I consider a mesh network the default for everyone, and if you’re still using a single non-mesh router you might want to know why. So let me explain.



















Quiz
8 Questions · Test Your Knowledge

Home Networking & Wi-Fi

Think you know your routers from your repeaters — put your home networking know-how to the ultimate test.

Wi-FiRoutersSecurityHardwareProtocols

What does the ‘5 GHz’ band in Wi-Fi offer compared to the ‘2.4 GHz’ band?

That’s right! The 5 GHz band delivers faster data rates but loses signal strength more quickly over distance and through walls. It’s ideal for devices close to the router that need maximum throughput, like streaming 4K video.

Not quite — the 5 GHz band actually offers faster speeds at the cost of range. The 2.4 GHz band travels farther and penetrates obstacles better, which is why smart home devices and older gadgets often prefer it.

Which Wi-Fi standard, introduced in 2021, is also known as Wi-Fi 6E and extends into a new frequency band?

Correct! 802.11ax is the technical name for Wi-Fi 6 and Wi-Fi 6E. The ‘E’ variant extends the standard into the 6 GHz band, offering a massive swath of new, less-congested spectrum for faster and more reliable connections.

The answer is 802.11ax — that’s Wi-Fi 6 and Wi-Fi 6E. Wi-Fi 6E adds support for the 6 GHz band, giving it far less congestion than the crowded 2.4 GHz and 5 GHz bands. 802.11be is actually the upcoming Wi-Fi 7 standard.

What is the default IP address most commonly used to access a home router’s admin interface?

Spot on! The vast majority of consumer routers use either 192.168.0.1 or 192.168.1.1 as the default gateway address. Typing either into your browser’s address bar will bring up the router’s login page — just make sure you’ve changed the default password!

The correct answer is 192.168.0.1 or 192.168.1.1. These are the most common default gateway addresses for home routers. The 255.x.x.x addresses are subnet masks, and 127.0.0.1 is your own machine’s loopback address, not a router.

Which Wi-Fi security protocol is considered most secure for home networks as of 2024?

Excellent! WPA3 is the latest and most robust Wi-Fi security protocol, introduced in 2018. It uses Simultaneous Authentication of Equals (SAE) to replace the older Pre-Shared Key handshake, making it far more resistant to brute-force attacks.

The answer is WPA3. WEP is completely broken and should never be used, WPA is outdated, and WPA2 with TKIP has known vulnerabilities. WPA3 offers the strongest protection, and if your router supports it, you should enable it right away.

What is the primary difference between a mesh Wi-Fi system and a traditional Wi-Fi range extender?

Exactly right! Mesh systems use multiple nodes that talk to each other intelligently, handing off your device seamlessly as you move around your home under one SSID. Traditional range extenders typically broadcast a separate network and can cut bandwidth in half as they relay the signal.

The correct answer is that mesh nodes form one intelligent, seamless network. Range extenders are actually the ones that often create separate SSIDs (like ‘MyNetwork_EXT’) and can significantly reduce speeds. Mesh systems are far superior for large homes with many devices.

What does DHCP stand for, and what is its main function on a home network?

Perfect! DHCP (Dynamic Host Configuration Protocol) is the unsung hero of home networking. Every time a device joins your network, your router’s DHCP server automatically hands it a unique IP address, subnet mask, and gateway info so it can communicate without manual configuration.

DHCP stands for Dynamic Host Configuration Protocol, and its job is to automatically assign IP addresses to devices on your network. Without it, you’d have to manually configure a unique IP address on every single phone, laptop, and smart device — a tedious nightmare!

What is ‘QoS’ (Quality of Service) used for in a home router?

That’s correct! QoS lets you tell your router which traffic gets priority. For example, you can prioritize video calls or gaming over a family member’s file download, ensuring your Zoom meeting doesn’t freeze just because someone is downloading a large update.

QoS — Quality of Service — is actually about traffic prioritization. By tagging certain data types (like VoIP calls or gaming packets) as high priority, your router ensures latency-sensitive applications get bandwidth first, even when the network is congested.

What does the ‘WAN’ port on a home router connect to?

Correct! WAN stands for Wide Area Network, and the WAN port is where your router connects to the outside world — typically to your cable modem, DSL modem, or ISP gateway. The LAN ports on the other side connect to devices inside your home network.

The WAN (Wide Area Network) port connects your router to your ISP’s modem or gateway — essentially your entry point to the internet. The LAN (Local Area Network) ports are for connecting devices inside your home. Mixing them up can cause your network to not function at all!

Challenge Complete

Your Score

/ 8

Thanks for playing!

Mesh Wi-Fi solves a problem most homes already have

The internet is no longer confined to one spot in your home

In the early days of home internet, there was no real reason to have Wi-Fi coverage all over your home. You installed the router in your home office, or near the living room, and that was enough. People didn’t have smartphones, tablets, or smart home devices that all needed access to the LAN.

As Wi-Fi devices proliferated, that central router became a problem. There’s only so much power you can push into the antennas, and the inverse square law drains that signal of power in very short order.

It was a problem that had many suboptimal solutions. Wi-Fi repeaters destroy performance, access points need long Ethernet runs, and Powerline Ethernet only works well in ideal conditions. Most older homes can’t provide that with their aging wiring. In short, trying to expand a central router’s reach has usually involved some janky mishmash of solutions.

A modern mesh router kit just solved that problem without any fuss. The biggest problem you’ll have is how to position them. Everything else is usually just handled automatically.

Brand

eero

Range

1,500 sq. ft.

Mesh Network Compatible

Yes

The eero 6 mesh Wi-Fi router allows you to upgrade your home network without breaking the bank. Compatible with the wider eero ecosystem, you’ll find that this node can either start or expand your wireless network with ease.


Mesh systems prioritize consistency over peak speed

Good enough internet everywhere

Top view of the contents of the Netgear Nighthawk MK93S mesh system. Credit: Jordan Gloor / How-To Geek

I think it’s important to point out that with Wi-Fi it’s much more important to get consistent and reliable performance wherever you are in your home than to hit crazy peak speeds. Sure, if you buy an expensive router, you can blast data when you’ve got line of sight and are a few feet away, but then you might as well just connect to it with an Ethernet cable.

For the price of one very fast centralized router, you can buy an entry-level mesh router kit and have fast enough internet everywhere, and never have to think about it again. I’m still running a Wi-Fi 5 mesh system in my two-storey rental home and I get 200+ Mbps minimum anywhere. If I need more speed than that on a single device, it’s going on Ethernet.

As prices come down on Wi-Fi 6 and 7 mesh systems, we’ll all eventually get access to that gigabit or better wireless tier, but I’d rather have a few hundred Mbps everywhere rather than a few Gbps in just one place and zero internet elsewhere.

Setup and management are finally user-friendly

Your dog could do it if it had thumbs

TP-Link Deco Mesh Wi-Fi Puck sitting on a desk beside two stacked books Credit: TP-Link

It’s hard to overstate just how easy modern mesh routers are to set up. After you’ve got the first unit up, usually by using a mobile app, adding more is generally just a matter of turning them on close to any previously activated router and waiting a few seconds.

As for the actual management of the network, on my TP-Link system you can see the topology of your network, how the pods are doing in terms of bandwidth, and you can automatically optimize for network interference and signal strength. The days of cryptic and largely manual router configuration are over. Even port forwarding, which has always tripped me up on old routers, now just works with a few taps on my phone screen.

The price argument doesn’t hold up anymore

There’s something for every budget

The biggest reason I think people have avoided mesh systems is cost. That’s perfectly fair, because mesh systems are more expensive than a single router. The thing is, prices have come down significantly, especially for mesh on older Wi-Fi standards.

But, even if you want newer Wi-Fi like 6E or 7, you don’t have to start your mesh journey with a full kit. You can buy a single mesh router, use that as your primary, and then add more as you can afford it. Even better, if you’ve bought a new router recently, there’s a chance it already supports mesh technology. It doesn’t even have to be that recent, since some older routers have gained mesh capability thanks to firmware updates.

If you already have a router that’s mesh-capable, then extending your home network any other way would be silly. Also, keep in mind that all the routers in your mesh network don’t have to be identical. That’s a common misconception, but the only thing they need to have in common is support for the same mesh technology. Just keep in mind that your performance will only be as good as the slowest device in the chain.


Mesh is for everyone

The bottom line is that mesh network technology is now cheap enough, mature enough, and easy enough that I honestly think everyone should have a good reason not to use it rather than looking for reason to use it. Wi-Fi should be like water or electricity. You want everyone in your home to have easy access to it no matter where they are. Mesh will do that for you.

The Unifi Dream Router 7.

9/10

Brand

Unifi

Range

1,750 square feet

The Unifi Dream Router 7 is a full-fledged network appliance offering NVR capabilities, fully managed switching,a built-in firewall, VLANs, and more. With four 2.5G Ethernet ports (one with PoE+) and a 10G SFP+ port, the Unifi Dream Router 7 also features dual WAN capabilities should you have two ISP connections. It includes a 64GB microSD card for IP camera storage, but can be upgraded for more storage if needed. With Wi-Fi 7, you’ll be able to reach up to a theoretical 5.7 Gbps network speed when using the 10G SFP+ port, or 2.5 Gbps when using Ethernet. 




Source link