xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

bash variables plus operator All In One

bash variables plus operator All In One

Errors ❌

missing pass params

#!/usr/bin/env bash

# echo 🎉 emoji ^-v-^
# echo "🎉 emoji ^-v-^"

# = 两边不可以有空格 ❌
# arg1 = $1
# OK, no space ✅
arg1=$1
arg2=$2
sum=$(($arg1 + $arg2))

echo $arg1
echo $arg2
# -e 换行
echo -e "\n"
echo $sum


#!/bin/bash

# OK, no space ✅
arg1=$1
arg2=$2
sum=$(($arg1 + $arg2))

echo $arg1
echo $arg2
# -e 换行
echo -e "\n"
echo $sum

Solutions ✅

just need pass args OR 参数可以为空 ✅

#!/usr/bin/env bash

# echo 🎉 emoji ^-v-^
# echo "🎉 emoji ^-v-^"

# = 两边不可以有空格 ❌
# arg1 = $1
# OK, no space ✅
arg1=$1
arg2=$2
# 🚀 ✅, 参数可以为空
sum=$((arg1 + arg2))
# OR
# 👎✅,  参数不可以为空
# sum=$(($arg1 + $arg2))

echo $arg1
echo $arg2
# -e 换行
echo -e "\n"
echo $sum

# DEMO
# ./sum.sh 1 2

#!/bin/bash

# OK, no space ✅
arg1=$1
arg2=$2
# 🚀 ✅, 参数可以为空
sum=$((arg1 + arg2))
# OR
# 👎✅,  参数不可以为空
# sum=$(($arg1 + $arg2))

echo $arg1
echo $arg2
# -e 换行
echo -e "\n"
echo $sum

# DEMO
# ./add.sh 1 2

bash add operator

#!/bin/bash


#!/usr/bin/env bash

arg1=$1
arg2=$2

# ✅
num=$(($arg1 + $arg2))

# ✅
str=$((arg1 + arg2))


echo "num: \$arg1 + \$arg2 = $num"
# -e 换行
echo -e "\n"
echo "str: arg1 + arg2 = $str"

# DEMO
# ./num.sh 1 2

# num: $arg1 + $arg2 = 3

# str: arg1 + arg2 = 3


#!/usr/bin/env bash

arg1=$1
arg2=$2

# ✅
num=$(($arg1 + $arg2))

# ✅
str=$((arg1 + arg2))


echo "num: \$arg1 + \$arg2 = $num"
# -e 换行
echo -e "\n"
echo "str: arg1 + arg2 = $str"

# DEMO
# ./str.sh 1 2

# num: $arg1 + $arg2 = 3

# str: arg1 + arg2 = 3

https://github.com/xgqfrms/linux/blob/master/zsh/num.sh

https://github.com/xgqfrms/linux/blob/master/zsh/str.sh

refs

https://www.imooc.com/notepad/2582fb

https://www.imooc.com/u/1066707/notepad/336

https://www.howtogeek.com/442332/how-to-work-with-variables-in-bash/



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


posted @ 2021-01-05 16:12  xgqfrms  阅读(91)  评论(2编辑  收藏  举报