Fun fact: you can define a shell function which uses a subshell like so:

my_func() (
do_the_thing
)

No need for { ( ) }

Follow

Fun fact v2: any statement can be the body of a shell function

foobar() case "$1" in
*)
echo hi
;;
esac

@sir that's a really fun fact I will keep it in my fact book

@sir does a pipeline count as a statement?

eg.
foobar() ls -l |grep head

@Wolf480pl no. That's considered a command-list (in grammar terms), and the function takes a compound-command. There's an explicit list of compound-commands here:

pubs.opengroup.org/onlinepubs/

You can, however, perma-redirect a function to a file or file handle, so this works:

foobar() ls -l >example

The following redirections are valid here:

pubs.opengroup.org/onlinepubs/

Sign in to participate in the conversation
Mastodon

cmpwn.com is a private Mastodon instance for friends of SirCmpwn.