shell 命令返回值 传递

标准输出

方法一:

注意cat命令外面的那个引号是反引号,键盘上数字1旁边那个

xxx@xxx-desktop:~/temp$ aaa=`cat 123`
xxx@xxx-desktop:~/temp$ echo $aaa
abc
 


方法二:

xxx@xxx-desktop:~/temp$ ccc=$(cat 123)
xxx@xxx-desktop:~/temp$ echo $ccc
abc

 

返回值

$ cat test3
#!/bin/bash
return 123

$ source test3

$ echo $?
123

# `.` 和 source 是一样的
$ . test3 

$ echo $?
123

# 直接运行 test3, 或者从bash 运行会报错,只能用source 执行
$ ./test3
./test3: line 2: return: can only `return' from a function or sourced script

$ bash test3
./test3: line 2: return: can only `return' from a function or sourced script

 

 

 

你可能感兴趣的:(linux)