0

How can I use a bashrc command like below

  # .bashrc file
  # google_speech
  say () {
    google_speech -l en "$1"
  }

as a string of text, since the above code only reads out the first word of the sentence or paragraph i paste.

like for example if i go into terminal and type:

  $ say hello friends how are you

then the script only thinks i typed

  $ say hello
Johnny5
  • 19
  • 2

1 Answers1

0

Try to use "$@" (with double quotes arround) to get all the arguments of your function:

$ declare -f mysearch # Showing the definition of mysearch (provided by your .bashrc)
mysearch () 
{ 
    echo "Searching with keywords : $@"
}
$ mysearch foo bar # execution result
Searching with keywords : foo bar

Function or scripts arguments are like arrays, so you can use:

1) $# / ${#array[@]} to getting the number of arguments / array's elements.

2) $1 / ${array[1]} to getting the first argument / array's element.

3) $@ / ${array[@]} to getting all arguments / array's elements.

EDIT: according to chepner's comment:

Using $@ inside a larger string can sometimes produce unintended results. Here, the intent is to produce a single word, so it would be better to use $*

And here's a good topic with great answers explaining the differences.

EDIT 2: Do not forget to put double quotes around $@ or $*, maybe your google_speach is taking only one arg. Here's a demo to give you a better understanding:

$ mysearch ()  {      echo "Searching with keywords : $1"; }
$ mysearch2 ()  {     mysearch "$*"; }
$ mysearch2 Hello my dear
Searching with keywords : Hello my dear
$ mysearch3 ()  {     mysearch $*; } # missing double quotes
$ mysearch3 Hello my dear
Searching with keywords : Hello # the other arguments are at least ignored (or sometimes the program will fail when he's controlling the args).
Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32
  • Using `$@` inside a larger string can sometimes produce unintended results. Here, the intent is to produce a single word, so it would be better to use `$*`. – chepner Jun 24 '18 at 15:44
  • @ chepner please re-read my question. the intent was for an entire string of words, not a single word... – Johnny5 Jun 25 '18 at 19:30
  • Idriss thank you so much for your time, it didnt work ... however YOUR ANSWER IS COMPLETELY CORRECT, so thank you again!... for some reason it wont work with this "google_speech" command. the error I got back said `$ "google_speech: error: unrecognized arguments: are you"` when I gave it `$ say how are you` after using your code in my .bashrc. so its still only grabbing the first word of what i give it. however at least you got the command to see the words now, except its just not processing them for some reason and giving the error... why won't it work with this "google_speech" command? – Johnny5 Jun 25 '18 at 19:34
  • @Johnny5 I've edited my answer (look at the **edit 2** part). – Idriss Neumann Jun 26 '18 at 06:53