Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- script ---
- #!/bin/bash
- echo "a" > a.txt
- echo "b" >> a.txt
- echo "c" >> a.txt
- echo "d" >> a.txt
- fun1()
- {
- local string="$1"
- local line
- cat a.txt | while read line; do
- if [ "$line" == "$string" ]; then
- echo "$string: found"
- return
- fi
- done
- echo "$string: not found"
- }
- fun1 123
- fun1 b
- fun1 456
- --- output ---
- 123: not found
- b: found
- b: not found
- 456: not found
- --- explanation ---
- The pipe ("|") spawns a subshell, a separate bash
- process and the "return" inside it exits the
- subshell not the function.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement