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

Doing function in a subshell #54

@ivbit

Description

@ivbit

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions