Is production: true
#shell

Title: Snippet to Execute Script From Stdin

Created: 29 Mar 2023 Modified: 29 Mar 2023

Description: Unlock the power of shell scripts with this comprehensive guide to using sh, bash, and source commands to execute your code. Discover how to pass parameters to your scripts, process substitutions, and handle input from both files and web requests. Whether you're a seasoned developer or just getting started with shell scripting, this article will help you take your skills to the next level.



We could execute command into bash in different form from stdin, file and web request.

read from file

bash file-script.sh
# or
sh file-script.sh
# or
source file-script.sh

read from file with parameter

bash file-script.sh {param}
# or
sh file-script.sh {param}
# or
source file-script.sh {param}

pass through pipeline

cat file-script.sh | bash
# or
cat file-script.sh | sh

# source doesn't support pipeline

pass through process subsitution

bash <(cat file-script.sh)
# or
sh <(cat file-script.sh)
# or
source <(ca file-script.sh)

pass through process subsitution with parameter

bash <(cat file-script.sh) {parameter}
# or
sh <(cat file-script.sh) {parameter}
# or
source <(ca file-script.sh) {parameter}

We could also pull a shell script from web request.

bash <(curl {url})
# or 
sh <(curl {url})
# or
source <(curl {url})