Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Doing function in a subshell #54

Open
ivbit opened this issue Jan 1, 2024 · 0 comments
Open

Doing function in a subshell #54

ivbit opened this issue Jan 1, 2024 · 0 comments

Comments

@ivbit
Copy link

ivbit commented Jan 1, 2024

Here https://github.com/dylanaraps/pure-sh-bible/#split-a-string-on-a-delimiter and in other examples:
No need to restore the value of IFS, or to restore options.
Use parentheses () instead of curly braces {} to run a function in a subshell.
It will not pollute the environment.

split() ( 
    set -f
    IFS=$2
    set -- $1
    printf '%s\n' "$@"
)

Doing redirects before commands helps to avoid ambiguity:

>|listing.txt ls -lhAF
>|listing.txt echo Hello world
>|listing.txt 2>|errors.txt ls -lhAF

Some commercial Unix POSIX shell implementations require using $ variable reference in $(( )) arithmetic:

$ var=1
$ var=$(( $var + 2023 ))
$ printf '%s\n' "$var"

var=$((var+2023)) and : $((var+=2023)) will not work on those systems

I made some notes about portable POSIX-compliant shell scripting:

https://ivanb.neocities.org/blogs/y2024/posix

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant