Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.
#function myfunc
myfunc(){
echo $1
case $1 in

    e)
            a=5
            ;;
    q)
            a=10
            ;;

esac
}

myfunc

I need help in following :

$myfunc.sh e

echo $1 is not showing anything. case is also not working. What am I doing wrong?

share|improve this question
3  
The problem is, inside the function $1, $2, ... are function arguments not the script arguments, see: stackoverflow.com/questions/3966048/… –  Jason Conti Aug 3 '13 at 19:13
1  
To follow up on Jason's comment, the solution is myfunc "$@" –  glenn jackman Aug 4 '13 at 1:14
add comment

1 Answer 1

You forgot to pass at least one parameter to the myfunc function when you call it. So, your myfunc.sh script should look like:

#!/bin/bash

#function myfunc
myfunc(){
    echo $1
    case $1 in
        e) a=5 ;;
        q) a=10 ;;
        *) a='not e or q' ;;
    esac
    echo $a
}

myfunc $1   #in this case you can also use $@ or $* 

More about:

share|improve this answer
    
If you are not sure about a variable value (it could contain spaces) it is always better to enclose it between quotes i.e. myfunc "$1" or myfunc "$@". –  pabouk Aug 12 '13 at 12:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.