开发者

Using Return in bash

I'm wanting this script to print 1,2,3.. without the use of functions, just execute two.sh then carry on where it left off, is it possible?

开发者_StackOverflow中文版
[root@server:~]# cat testing.sh                                  
#!/bin/bash
echo "1"
exec ./two.sh
echo "3"

[root@server:~]# cat two.sh                                     
#!/bin/bash
echo "2"
return


exec, if you give it a program name a, will replace the current program with whatever you specify.

If you want to just run the script (in another process) and return, simply use:

./two.sh

to do that.

For this simple case, you can also execute the script in the context of the current process with:

. ./two.sh

That will not start up a new process but will have the side-effect of allowing two.sh to affect the current shell's environment. While that's not a problem for your current two.sh (since all it does is echo a line), it may be problematic for more complicated scripts (for example, those that set environment variables).


a Without a program name, it changes certain properties of the current program, such as:

exec >/dev/null

which simply starts sending all standard output to the bit bucket.


Sure, just run:

echo "1"
./two.sh
echo "3"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜