Linux shell set command All In One

Posted xgqfrms

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux shell set command All In One相关的知识,希望对你有一定的参考价值。

Linux shell set command All In One set -u set +u

Linux shell set command All In One

erros ❌

unbound-variable

# $1: unbound variable
if (($1))
then
  echo $1
  # arg=$1
  # tsGenerator $arg
  # tsGenerator $1
  # tsGenerator
else
  echo "❌"
fi

#!/usr/bin/env bash

# 显示所有的已经执行的命令
# set -eux
# set -ex

# bug
set -u

echo "\\$1 = $1"

echo "\\$2 = $2"

# 一个参数
# $ /auto-ts-files-generator.sh 33

# 两个参数
# $ ./auto-ts-files-generator.sh 33 regular-expressions

It is well appreciated that when set -u is active Bash will report an error if an unbound variable is referenced, e.g.:

很好理解,当 set -u 处于活动状态时,如果引用未绑定变量,Bash 将报告错误,例如:

https://bnikolic.co.uk/blog/bash-unbound-variable

https://replit.com/@bnwebcode/bash-unbound-variable

https://unix.stackexchange.com/questions/463034/bash-throws-error-line-8-1-unbound-variable

: bad substitution

# : bad substitution ❌
if [ -n "$$2:-" ]; then
  echo "\\$2 = $2"
  filename=$2
else
  filename=$fallback
fi

solutions

set +u

-u Treats unset parameters as an error when substituting.
Using + rather than - causes these flags to be turned off.

https://blog.csdn.net/qq_21481459/article/details/104202174

demos

#!/usr/bin/env bash

# 显示所有的已经执行的命令
set -eux

# bug ❌ https://www.cnblogs.com/xgqfrms/p/17251644.html#5161214
# 当 set -u 处于活动状态时,如果引用了未绑定的变量,Bash 将报告错误
# set -u\\
# ✅
set +u

echo "

以上是关于Linux shell set command All In One的主要内容,如果未能解决你的问题,请参考以下文章

[Python] 利用commands模块执行Linux shell命令

Linux shell command学习笔记

Linux Shell command (Quick Study)

Linux shell command学习笔记

Unix/Linux shell脚本中 “set -e” 的作用

Unix/Linux shell脚本中 “set -e” 的作用