Skip to content

Instantly share code, notes, and snippets.

@benolee
Created March 4, 2023 18:54
Show Gist options
  • Save benolee/c9388a30c85e27354316e76003c37bdf to your computer and use it in GitHub Desktop.
Save benolee/c9388a30c85e27354316e76003c37bdf to your computer and use it in GitHub Desktop.

put this in lambda.sh

#!/usr/bin/env bash

: ${temp_directory=$(mktemp -d "/tmp/demo/example_XXXXXXXXXXXXX")}
: ${filename=$(mktemp "$temp_directory/command_XXXXXXXXXXXX")}
export PATH="$temp_directory:$PATH"

lambda() {
  {
    echo "#!/usr/bin/env bash"
    echo ""
    echo "$1"
  } > "$filename"
  chmod +x "$filename"
  echo "$filename"
}

the lambda function generates a valid bash script in a random filename in PATH, and echoes the filename

$ source ./lambda.sh
$ lambda 'echo you said: "$@"'
/tmp/demo/example_L2HH5aaWSeTRi/command_FJyhq4slC0hn

$ cat /tmp/demo/example_L2HH5aaWSeTRi/command_FJyhq4slC0hn
#!/usr/bin/env bash

echo you said: "$@"

you can run the file, of course

$ /tmp/demo/example_L2HH5aaWSeTRi/command_FJyhq4slC0hn hey there
you said: hey there

but you don't really even need to know what the filename is

$ "$(lambda 'echo you said: "$@"')" foo bar baz
you said: foo bar baz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment