Is production: true
#shell

Snippet to Execute Script From Stdin

Created: 29 Mar 2023 Modified: 29 Mar 2023 Lang: English

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

1
2
3
4
5
bash file-script.sh
# or
sh file-script.sh
# or
source file-script.sh

read from file with parameter

1
2
3
4
5
bash file-script.sh {param}
# or
sh file-script.sh {param}
# or
source file-script.sh {param}

pass through pipeline

1
2
3
4
5
cat file-script.sh | bash
# or
cat file-script.sh | sh

# source doesn't support pipeline

pass through process subsitution

1
2
3
4
5
bash <(cat file-script.sh)
# or
sh <(cat file-script.sh)
# or
source <(ca file-script.sh)

pass through process subsitution with parameter

1
2
3
4
5
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.

1
2
3
4
5
bash <(curl {url})
# or 
sh <(curl {url})
# or
source <(curl {url})