Help needed in passing arguments from command line in bash

#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?

2

1 Answer

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:

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like