I prefer using named variables instead of positional arguments. So the logic of my code is based on names instead of indexes, such as $1, $2 and so on. This way, even if the order of given arguments is changed, I still maintain my function's logic.
And of course, let's learn by example; assuming we create the greet() function
Now, I want to change the positional arguments, so the function consumes age and then name
greet(){# I switched between $1 and $2# Also changed the order of variables so it makes senselocal age="$1"local name="$2"# Didn't touch the logicecho"Hello ${name}, you're ${age} years old."}# Usage
greet "33""Willy"
I hope that explains it
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
@zoulja Good point!
I prefer using named variables instead of positional arguments. So the logic of my code is based on names instead of indexes, such as
$1
,$2
and so on. This way, even if the order of given arguments is changed, I still maintain my function's logic.And of course, let's learn by example; assuming we create the
greet()
functionNow, I want to change the positional arguments, so the function consumes
age
and thenname
I hope that explains it