3

Using a bash script on Mac OS X, how do I combine echo, say and read to make it cleaner and less line of code? The below script works but to me there are too many lines. Thanks a lot for your help.

#!/usr/bin/env bash

clear

echo -n "Enter your first name: "
say "Enter your first name"
read -r name
echo -n "Enter your last name: "
say "Enter your last name"
read -r lastname
say "Good morning ${name}${lastname}"
1

You can define functions in bash:

echoAndSay() {
    say $*
    printf "%s: " "$*"
}

echoAndSay "Enter your first name"
read -r name
echoAndSay "Enter your last name"
read -r lastname
say "Good morning ${name} ${lastname}"
  • Thanks nohillside it is much better like that. When I run the first time I received an error message, I think it suppose to be like that: printf "%s:" "$*" which I did and it work. Thanks again. – Fabio Viola Aug 6 at 12:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .