32.6k star,后悔没有早点知道这个开源工具--shellcheck

简介: 【8月更文挑战第8天】

1 shellcheck 简介

今天发现的一款神器,如果你日常会接触到shell脚本,或者说自己需要写一些shell脚本,那么强烈建议你用下这个工具。

shellcheck一个静态的shell脚本分析工具,可以判断脚本哪里有异常,哪里可以优化,并且会给出对应的解决办法。几年前我咋没发现这个,后悔。。。

一个简单的示例:
image.png

这款工具不仅适用于初学者,对于中高级使用者帮助更大。

2 安装

github可以访问的直接到如下链接去下载就可以,目前支持多个终端,也有web可访问

https://github.com/koalaman/shellcheck

github如果无法访问的话,可以后台直接私信

web访问地址:
https://www.shellcheck.net/

使用方式:

image.png

3 shell脚本中的一些常见错误

  1. 常见错误引用
    echo $1                           # Unquoted variables
    find . -name *.ogg                # Unquoted find/grep patterns
    rm "~/my file.txt"                # Quoted tilde expansion
    v='--verbose="true"'; cmd $v      # Literal quotes in variables
    for f in "*.ogg"                  # Incorrectly quoted 'for' loops
    touch $@                          # Unquoted $@
    echo 'Don't forget to restart!'   # Singlequote closed by apostrophe
    echo 'Don\'t try this at home'    # Attempting to escape ' in ''
    echo 'Path is $PATH'              # Variables in single quotes
    trap "echo Took ${SECONDS}s" 0    # Prematurely expanded trap
    unset var[i]                      # Array index treated as glob
    
  2. 常见错误条件语句

    [[ n != 0 ]]                      # Constant test expressions
    [[ -e *.mpg ]]                    # Existence checks of globs
    [[ $foo==0 ]]                     # Always true due to missing spaces
    [[ -n "$foo " ]]                  # Always true due to literals
    [[ $foo =~ "fo+" ]]               # Quoted regex in =~
    [ foo =~ re ]                     # Unsupported [ ] operators
    [ $1 -eq "shellcheck" ]           # Numerical comparison of strings
    [ $n && $m ]                      # && in [ .. ]
    [ grep -q foo file ]              # Command without $(..)
    [[ "$$file" == *.jpg ]]           # Comparisons that can't succeed
    (( 1 -lt 2 ))                     # Using test operators in ((..))
    [ x ] & [ y ] | [ z ]             # Accidental backgrounding and piping
    
  3. 误用的命令

    grep '*foo*' file                 # Globs in regex contexts
    find . -exec foo {} && bar {} \;  # Prematurely terminated find -exec
    sudo echo 'Var=42' > /etc/profile # Redirecting sudo
    time --format=%s sleep 10         # Passing time(1) flags to time builtin
    while read h; do ssh "$h" uptime  # Commands eating while loop input
    alias archive='mv $1 /backup'     # Defining aliases with arguments
    tr -cd '[a-zA-Z0-9]'              # [] around ranges in tr
    exec foo; echo "Done!"            # Misused 'exec'
    find -name \*.bak -o -name \*~ -delete  # Implicit precedence in find
    # find . -exec foo > bar \;       # Redirections in find
    f() { whoami; }; sudo f           # External use of internal functions
    
  4. 初学者常见错误
    var = 42                          # Spaces around = in assignments
    $foo=42                           # $ in assignments
    for $var in *; do ...             # $ in for loop variables
    var$n="Hello"                     # Wrong indirect assignment
    echo ${var$n}                     # Wrong indirect reference
    var=(1, 2, 3)                     # Comma separated arrays
    array=( [index] = value )         # Incorrect index initialization
    echo $var[14]                     # Missing {} in array references
    echo "Argument 10 is $10"         # Positional parameter misreference
    if $(myfunction); then ..; fi     # Wrapping commands in $()
    else if othercondition; then ..   # Using 'else if'
    f; f() { echo "hello world; }     # Using function before definition
    [ false ]                         # 'false' being true
    if ( -f file )                    # Using (..) instead of test
    
相关文章
|
Oracle Java 关系型数据库
在 macOS 上安装 JDK 17
在 macOS 上安装 JDK JDK 支持基于 Intel (x64) 和 Apple Silicon (AArch64) 的 Mac 电脑。 本主题包括以下部分: 在 macOS 上安装 JDK 的系统要求 macOS JDK 安装说明符号 确定 macOS 上的默认 JDK 版本 在 macOS 上安装 JDK 在 macOS 上卸载 JDK macOS 安装常见问题
10552 110
|
安全 开发工具
VBA窗体最大化最小化按钮实现
VBA窗体最大化最小化按钮实现
1096 0
|
Python
python使用pip镜像源加速安装包(清华、阿里、中科大)
python使用pip镜像源加速安装包(清华、阿里、中科大)
10589 0
python使用pip镜像源加速安装包(清华、阿里、中科大)
|
数据可视化 大数据 数据挖掘
Apache Superset 1.2.0教程 (一)—— 安装(Windows版)
Apache Superset 是一款由 Airbnb 开源的“现代化的企业级 BI(商业智能) Web 应用程序”,其通过创建和分享 dashboard,为数据分析提供了轻量级的数据查询和可视化方案。 近日推出了全新的 1.2.0版本,本教程也就从头开始讲解Apache Superset的使用。
3385 0
Apache Superset 1.2.0教程 (一)—— 安装(Windows版)
|
3月前
|
缓存 JSON API
Qwen3.8‑Flash深度实战:新一代MoE架构、百万上下文、Agent工具调用API完整实操教程
Qwen3.8‑Flash作为兼顾性能与成本的新一代多模态基座,依托GDN+QSA混合稀疏注意力下一代MoE架构,实现百万级上下文窗口、原生图文视频多模态、完整Function‑Calling智能体闭环、上下文缓存、结构化输出等全套能力。推理激活参数量小,在保证绝大多数业务效果的前提下压低调用开销,尤其适合高并发线上业务、长文档解析、Agent自动化、代码辅助、多模态分析场景。
597 0
|
4月前
|
人工智能 前端开发 API
DeepSeek+WorkBuddy,下一代Office工作台来了!
微软还没做到的事,腾讯WorkBuddy先做到了。Office彻底进入Agent时代!
712 0
|
4月前
|
Web App开发 Go 开发者
我编写了一个手机遥控在线 PPT 翻页工具【Chrome 免费插件】
这是一款开源免费的Chrome插件,支持腾讯文档、Google Slides和微软在线PPT。手机扫码即变遥控器,实现无线翻页、实时画面同步、演讲备注查看、激光标注、观众互动等功能,无需APP,零延迟、高隐私、全免费。
612 0
|
9月前
|
人工智能 弹性计算
阿里云权益中心2026年最新:个人、学生及企业用户上云补贴及活动政策解读
2026阿里云最新权益汇总:新用户38元抢轻量服务器,老用户99元享ECS,企业199元起;学生领300元无门槛券;开通百炼免费得7000万tokens;AI先锋企业享1300元抵扣金。优惠券速领→aly.wiki(239字)
1307 12
|
9月前
|
编解码 文字识别 安全
AutoGod:安卓5-16全兼容!一站式自动化框架,开发效率直接拉满
Auto-God是一站式安卓自动化框架,兼容Android 5–16,覆盖手势、视觉(OCR/YOLO)、网络、UI(Material3悬浮界面)、拓展及安全(防HOOK/抓包/破解)全能力,开箱即用,真机/模拟器/云手机全支持,让自动化开发更简单、高效、安全。
1928 1
|
12月前
|
人工智能
基于vite7.2+vue3.5+deepseek-v3.2高颜值流式ai会话助手
基于vue3.5+vite7.2+vant4+markdown+openai深度集成deepseek-v3.2聊天大模型。支持浅色+深色主题、stream流式输出、代码高亮、复制代码、katex公式、mermaid图表等功能。
586 4