PFKrang
PFKrang

Reputation: 289

bash script echo arguments into separate lines

I have to write this script to separate arguments entered in the command line. Here is the script so far:

# template.sh
function usage
{
  echo "usage: $0 arguments ..."
  if [ $# -eq 1 ]
  then echo "ERROR: $1"
  fi
}

# Script starts after this line.

case $1 in
  *?)echo "My Name"
     date
     echo
     echo $*
     echo "*****"
     ;;
  *)usage
     ;;
esac

What I need to get is when I use the script it needs to look like:

$ ./template.sh peter frederick chang
My Name
Sun May 17 11:28:46 PDT 2015

peter
*****
frederick
*****
chang
*****

But instead I'm getting:

$ ./template.sh peter frederick chang
My Name
Sun May 17 11:28:46 PDT 2015

peter frederick chang
*****

Upvotes: 8

Views: 3483

Answers (1)

anubhava
anubhava

Reputation: 785186

Use printf instead of echo like this with "$@" to print arguments on different lines:

# Script starts after this line.
case $1 in
  *?)echo "My Name"
     date
     echo
     printf "%s\n*****\n" "$@"
     ;;
  *)usage
     ;;
esac

This will print:

My Name
Sun May 17 06:50:26 EDT 2015

peter
*****
frederick
*****
chang
*****

Upvotes: 8

Related Questions