User Tools

Site Tools

Return to Home page


How to pass arguments from a shell to a bash script

Implement a bash script that:

  • prints the number of arguments passed to the script
  • checks the number of arguments passed to the script and, in the case the number is different than 3, the script must print an error message before stopping the execution
  • prints the name of the script
  • prints the arguments passed to the script (in two different ways)

echo "Number of arguments: $#"
if [ $# -ne 3 ]
  echo "Usage: $0 arg1 arg2 arg3"
  exit 1
echo $0
echo $1 $2 $3
echo $*

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at]

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url:
/web/htdocs/ · Last modified: 2020/11/26 23:18 (external edit)

Privacy Policy