Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing script positional parameters inside a function

Tags:

bash

Test script:

#!/bin/bash
var=$1
function test {
echo "dollar1 $1"
echo "var $var"
}
test
exit

output

./test.sh d
dollar1
var d

Why isn't $1 populated inside the function, and is there a way to pass $1...$n to all functions without having them defined as a variable separately?

like image 564
bob dylan Avatar asked Oct 27 '25 09:10

bob dylan


1 Answers

The function has its own arguments. You can pass all arguments to a function using "$@", which expands to all the positional parameters in order:

test "$@"
like image 150
Michael Hoffman Avatar answered Oct 29 '25 23:10

Michael Hoffman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!