$ myscript.sh first_arg second_arg
# myscript.sh
#!/bin/bash
echo $1 # output: first_arg
echo $2 # output: second_arg
# bash arguments cheatsheet
$1, $2, ... -> are the positional parameters.
"$@" -> is an array-like construct of all positional parameters, {$1, $2, $3 ...}.
"$*" -> is the IFS expansion of all positional parameters, $1 $2 $3 ....
$# -> is the number of positional parameters.
$- -> current options set for the shell.
$$ -> pid of the current shell (not subshell).
$_ -> most recent parameter (or the abs path of the command to start the current shell immediately after startup).
$IFS -> is the (input) field separator.
$? -> is the most recent foreground pipeline exit status.
$! -> is the PID of the most recent background command.
$0 -> is the name of the shell or shell script.
bash yourscript.sh file1 file2
yourscript.sh
inside script
#!/bin/bash
a=$1 # = to file1
b=$2 # = file2
echo $1
echo $2
cat >/tmp/demo-space-separated.sh <<'EOF'
#!/bin/bash
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-e|--extension)
EXTENSION="$2"
shift # past argument
shift # past value
;;
-s|--searchpath)
SEARCHPATH="$2"
shift # past argument
shift # past value
;;
--default)
DEFAULT=YES
shift # past argument
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
echo "FILE EXTENSION = ${EXTENSION}"
echo "SEARCH PATH = ${SEARCHPATH}"
echo "DEFAULT = ${DEFAULT}"
echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l)
if [[ -n $1 ]]; then
echo "Last line of file specified as non-opt/last argument:"
tail -1 "$1"
fi
EOF
chmod +x /tmp/demo-space-separated.sh
/tmp/demo-space-separated.sh -e conf -s /etc /etc/hosts