Skip to content

Instantly share code, notes, and snippets.

@achmadns
Created May 12, 2017 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achmadns/bc1b13193605a2dfcb42f2869d059f0d to your computer and use it in GitHub Desktop.
Save achmadns/bc1b13193605a2dfcb42f2869d059f0d to your computer and use it in GitHub Desktop.
Print and Iterate Shell Script Parameters
#!/bin/bash
: '
Example
$ ./print-param.sh 4 2 3 4 5 6
4 2 3 4 5 6
4
2
3
4
5
6
max val: 4
0
1
2
3
'
echo "Script name: $0"
# print all parameter
echo "Parameters: $*"
# save the arguments
args=("$@")
# backup the elements
ELEMENTS=${#args[@]}
for (( i=0;i<$ELEMENTS;i++)); do
echo ${args[${i}]}
done
# get the first parameter
max=${args[0]}
printf "max val: $max\n"
# finite loop up to first param -1
for (( i=0;i<$max;i++)); do
echo $i
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment