Positional Arguments In Bash VS Posh (Powershell)
1 min readOct 14, 2021
The aim of this page📝 is to compare positional args handling in bash VS posh (PowerShell) — as part of learning bash. Always good to get a feel of the differences between languages.
1. bash
all args with $*
$*
is a special variable holding all of the passed arguments to the script via CLI
prnt(){
echo "you passed me" $*
}
>>> prnt fd fd dssddd fdfd
you passed me fd fd dssddd fdfd
one arg with $<n>
$1
is a special variable that denotes the first arg passed to the script via CLI
prnt(){
echo "you passed me" $1
}
>>> prnt fd fd dssddd fdfd
you passed me fd
2. posh
all args with $args
- in PowerShell you don’t number the arguments passed, those go into
$args
array and you can iterate over them
Function search-google {
$query = 'https://www.google.com/search?q='
$args | % { $query = $query + "$_+" }
$url = $query.Substring(0, $query.Length - 1)
start "$url"
}
>>> search-google hello my friends
one $args[n]
function prr() {write-host $args[0]}
# prr rr tt ss
# >>> rr