4

i'm playing around with basic shell scripts and would like to know how to do the following if possible

i have created a basic script with a function and i want to call it when i type the main command with variables like first name and surname

source ./test.sh; talk $John $Smith

  function talk($firstName, $lastName)
{
        echo "hi! ${firstName} ${lastName}"
}

I cant seem to get it to work,not sure where im going wrong, i've tried reading up but getting confused

Robert Smith
  • 45
  • 1
  • 1
  • 6
  • As an aside -- the `function` keyword is not part of POSIX-standard function declaration syntax; it's a ksh extension picked up by bash (not guaranteed to be supported with `/bin/sh`, and not necessarily even a good idea to use in shells that *do* support it). See http://wiki.bash-hackers.org/scripting/obsolete – Charles Duffy Feb 28 '18 at 21:27

1 Answers1

5

This should not be necessary.

talk()
{
   echo "hi! $1 $1"
}

defines the function. After a source you can invoke it with

talk Hans Peter

In sh variables are not declared with a prefix $, but accessed that way:

a=5
echo $a
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
toornt
  • 199
  • 4