shell script pass arguments code example

Example 1: pass parameters to bash script

# Run the script with the arguments as you would for
# every other command.
# Example: ./script.sh arg1 arg2 

#!/bin/sh
echo "argument1: $1"
echo "argument2: $2"

# Output: 
# argument1: arg1
# argument2: arg2

Example 2: shell script get arguments

$ myscript.sh first_arg second_arg

# myscript.sh
#!/bin/bash
echo $1 # output: first_arg
echo $2 # output: second_arg

Example 3: run sh with parameter

> ./myscript myargument
myargument becomes $1 inside myscript.

Example 4: get additional parameters linux scripting

$ cat myscript
#!/bin/bash
echo "First arg: $1"
echo "Second arg: $2"
$ ./myscript hello world
First arg: hello
Second arg: world

Example 5: bash pass all arguments

source script.sh "$@"