Shell 传递参数

含有空格的参数,只需要在参数传递的时候加双引号就可以。

#!/bin/bash

echo "---------------with double quote ---------------"

echo "$1"

echo "$2"

echo "$3"

my_procedure() {

echo "$1"

echo "$2"

echo "$3"

}

my_procedure "$@"

echo "---------------withtout double quote ---------------"

echo $1

echo $2

echo $3

my_procedure() {

echo $1

echo $2

echo $3

}

my_procedure "$@"

echo "---------------withtout double quote by passing arguments---------------"

my_procedure $@

输入:

./test.sh "a b" "c d" "e f"

输出结果为:

---------------with double quote ---------------

a b

c d e

f g

a b

c d e

f g

---------------withtout double quote ---------------

a b

c d e

f g

a b

c d e

f g

---------------withtout double quote by passing arguments ---------------

a

b

c

前两部分无论在函数内外,加不加引号都一样,但是在函数传递的时候要保证加引号。

lalala lalala

a1.***[email protected]

5年前 (2021-10-02)

友情链接