Skip to content

Commit

Permalink
fix function return string
Browse files Browse the repository at this point in the history
How to Return a String from Bash Functions
https://linuxhint.com/return-string-bash-functions/
  • Loading branch information
52fhy authored Mar 21, 2022
1 parent fd14775 commit c197f2e
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions chapter5.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ function function_name () {
#!/bin/bash

hello(){
echo 'hello';
echo 'hello world';
}

hello
```
运行结果:
```
hello
hello world
```

调用函数只需要给出函数名,不需要加括号。
Expand All @@ -41,7 +41,7 @@ Shell 函数返回值只能是整数,一般用来表示函数执行成功与
#!/bin/bash

function hello(){
return 'hello';
return 'hello world';
}

hello
Expand All @@ -56,17 +56,19 @@ line 4: return: hello: numeric argument required
#!/bin/bash

function hello(){
return 'hello';
echo "hello world";
}

str=hello
str=$(hello)

echo $str
echo $str
```

运行结果:
```
hello
hello world
hello world
```

像删除变量一样,删除函数也可以使用 `unset` 命令,不过要加上 `.f` 选项,如下所示:
Expand Down Expand Up @@ -129,4 +131,4 @@ num=$(sum)

```

这样就可以取到返回值了。
这样就可以取到返回值了。

0 comments on commit c197f2e

Please sign in to comment.