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

1
  • 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 wiki.bash-hackers.org/scripting/obsolete Commented Feb 28, 2018 at 21:27

1 Answer 1

5

This should not be necessary.

talk() {
   echo "Hi! $1 $2"
}

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

talk Hans Peterson

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

a=5
echo $a
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.