shell脚本执行传递参数注意事项,$1参数消失

1:执行命名 ./test.sh dev, 通过$1 获取的参数,需要及时分配变量保存;不能够全局使用;
2:如果需要在后续的函数中使用 $1, 需要用 方法2,继续将 $1 当做参数传递

#!/bin/bash

function repo_init2()
{   
    echo "repo_init $1"		# $1无效了
    echo "repo_init2 $branch" # $branch 变量有效
}

if [ "$1" != "" ] ;  then
    branch=$1		# 此时 $1是有效的
    echo "start git pull $branch code $1"
    repo_init2          # 执行方法1
    # repo_init2 $1			# 执行方法2
else
    echo "please input branch"
fi

运行之后的日志

root@dujunqiu-dev:~/share/auto_test# ./test.sh dev
start git pull dev code dev
repo_init 
repo_init2 dev
root@dujunqiu-dev:~/share/auto_test# 

你可能感兴趣的:(linux与虚拟机,linux)