Skip to content

Commit c38ab2d

Browse files
committed
feat: improve sed support on macOS
1 parent ab37d35 commit c38ab2d

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

docs/libbash.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ fi
15261526
---------------------------------------------------------------
15271527
<a name="lb_edit"></a>
15281528
### lb_edit
1529-
Edit a file withe the `sed -i` command.
1529+
Edit a file with the `sed -i` command.
15301530

15311531
#### Usage
15321532
```bash

libbash.sh

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,11 +2250,7 @@ lb_edit() {
22502250
# usage error
22512251
[ $# -ge 2 ] || return 1
22522252

2253-
if [ "$lb__oldsed" = true ] ; then
2254-
sed -i '' "$@"
2255-
else
2256-
sed -i "$@"
2257-
fi
2253+
"${lb__sed[@]}" "$@"
22582254
}
22592255

22602256

@@ -3271,20 +3267,26 @@ lb_current_script_name=$(basename "$lb_current_script")
32713267
[ "$lb_current_os" != macOS ] || lb__format_print=false
32723268

32733269
# Test sed command
3270+
lb__sed=(sed -i)
32743271
sed --version &> /dev/null
32753272
case $? in
32763273
0)
3277-
# normal sed command
3278-
declare -r lb__oldsed=false
3274+
# normal sed command: do nothing
3275+
true
32793276
;;
32803277
127)
32813278
# command sed not found
32823279
lb_error "libbash.sh: [ERROR] cannot found sed command. Some functions will not work properly."
32833280
lb__load_result=2
32843281
;;
32853282
*)
3286-
# old sed command (mostly on macOS)
3287-
declare -r lb__oldsed=true
3283+
# check if GNU sed is installed (brew on macOS)
3284+
if lb_command_exists gsed ; then
3285+
lb__sed=(gsed -i)
3286+
else
3287+
# if not, old sed command (default on macOS, may not work properly with regex)
3288+
lb__sed=(sed -i "''")
3289+
fi
32883290
;;
32893291
esac
32903292

0 commit comments

Comments
 (0)