The short bash script below list all files and dirs in given directory and its sub. What does the $1/*
mean in the script? Please give me some references about it. Thanks
#!/bin/sh
list_alldir(){
for file in $1/*
do
if [ -d $file ]; then
echo $file
list_alldir $file
else
echo $file
fi
done
}
if [ $# -gt 0 ]; then
list_alldir "$1"
else
list_alldir "."
fi
$1 - The first argument sent to the script. $2 - The second argument sent to the script. $3 - The third argument... and so forth. $# - The number of arguments provided. $@ - A list of all arguments provided.
$# is the number of positional parameters passed to the script, shell, or shell function. This is because, while a shell function is running, the positional parameters are temporarily replaced with the arguments to the function. This lets functions accept and use their own positional parameters.
$# Stores the number of command-line arguments that were passed to the shell program. $? Stores the exit value of the last command that was executed. $0 Stores the first word of the entered command (the name of the shell program). $* Stores all the arguments that were entered on the command line ($1 $2 ...).
Linux / Unix Community $1 is the first commandline argument. If you run ./asdf.sh a b c d e, then $1 will be a, $2 will be b, etc. In shells with functions, $1 may serve as the first function parameter, and so forth.
It's the glob of the first argument considered as a directory
In bash scripts the arguments to a file are passed into the script as $0
( which is the script name ), then $1
, $2
, $3
... To access all of them you either use their label or you use one of the group constructs. For group constructs there are $*
and $@
. ($*
considers all of the arguments as one block where as $@
considers them delimited by $IFS
)
$1
means the first parameter.for file in $1/*
means loop with the variable file
having the value of the name of each file in the directory named in the first parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With