"Argument" vs. "Paramter"
First of all, let me clarify what I mean by:
if a function's arguments does not contain any parameters
Given the following ls
command:
$ ls -a sub_folder
I call -a sub_folder
the function's arguments and only -a
I call parameter (as explained here).
Question
Now, in my bash
script, how can I check if a function's arguments does not contain any parameters?
I thought of checking that no argument in $@
starts with a -
. But how could I do that?
I'm new to bash and therefore not experienced enough but in TypeScript I would do something like:
if (args.some(arg => arg.startsWith("-"))) {
# parameters found
} else {
# no parameters
}
What I want to do
I actually wanted to write my own ls
function to also displays how many folders and files there are (via count_info
). But this should only be displayed if I call my_ls
with a -l
parameter (in my case, all my aliases except l
use the -l
option).
In my bashrc
I have the following code:
alias l="my_ls" # the only call with no parameters
alias ll="l -l"
alias la="l -l --almost-all"
alias ls="l -lAsSh"
my_ls () {
/bin/ls -p --color=always --group-directories-first "$@"
if [ ! -z "$1" ]; then # check if there were parameters
count_info
fi
}
This works for all the normal alias calls l
, ll
, etc.
But as soon as I to something like l subfolder
my function does not work properly anymore as my check [ ! -z "$1" ]
fails.
Another solution would be to simply test if there is a -l
parameter but how can I do that? Especially with also having parameter combinations like -lAsSh
in mind.
$1
and$2
are the parameters that receive them. (Note that shell functions don't have explicit signatures; they sort of have implicit signatures of$1, $2, $3, ...
) The standard terms for arguments like-l
are "flag" and "option".